Skip to content

Instantly share code, notes, and snippets.

View eisbilen's full-sized avatar

Erdem İşbilen eisbilen

  • Ford Motor Company
  • UK - Brentwood
View GitHub Profile
@eisbilen
eisbilen / dictIterate.py
Created November 7, 2020 20:59
How to Loop Through a Dictionary
dict_YTL_currency = {'Pound': 0.08, 'Euro': 0.09, 'Usd': 0.12}
# iterating using .values()
for value in dict_YTL_currency.values():
print(value)
# iterating using .keys()
for key in dict_YTL_currency.keys():
print(key)
@eisbilen
eisbilen / dictMerge.py
Created November 7, 2020 19:17
How to Sort Items In a Dictionary
import collections
# sort a dictionary by value
currency = {'Pound': 0.087, 'Euro': 0.09, 'Usd': 0.12, 'Bangladeshi_Taka': 10.0, 'Afghani': 9.12, 'Romanian_leu': 2.08, 'Quatari_rial': 2.34, 'Somali_Shilling': 0.01, 'Serbian_Dinar': 0.08}
sorted_currency = {k: v for k, v in sorted(currency.items(), key=lambda item: item[1])}
reversed_sorted_currency = {k: v for k, v in sorted(currency.items(), key=lambda item: item[1], reverse = True)}
print("dict original :" + str(currency))
print("dict sorted by value :" + str(sorted_currency))
print("dict reversed sorted by value:" + str(reversed_sorted_currency))
@eisbilen
eisbilen / dictMerge.py
Last active November 7, 2020 18:15
How to Merge Dictionaries
import sys
print("Python version")
print (sys.version)
currency_1 = {'Pound': 0.091, 'Euro': 0.093, 'Usd': 0.121}
currency_2 = {'Pound': 0.083, 'Afghani': 9.12, 'Romanian_Leu': 2.08}
# for Python 3.5 or greater
# new dictionary is a shallowly merged dictionary of currency_1 and currency_2
# with values from currency_2 replacing those from currency_1
import copy
# YTL Currency
dict_YTL_currency = {'Pound': [0.091, 0.087], 'Euro': 0.093, 'Usd': 0.121}
# create a shallow copy of the original dictionary
newDict = dict_YTL_currency.copy()
# modify the primitive datatype value in the new dictionary
newDict["Euro"] = 0.081
@eisbilen
eisbilen / dictRemoveItems.py
Last active November 7, 2020 13:05
How to Remove an Item From a Dictionary
# YTL Currency
dict_YTL_currency = {'Pound': 0.087, 'Euro': 0.09, 'Usd': 0.12}
print ("dict original : " + str(dict_YTL_currency))
# using del to remove a dict
del dict_YTL_currency['Pound']
print ("dict after removal : " + str(dict_YTL_currency))
# using del to remove a dict
# raises exception if the key does not exist
@eisbilen
eisbilen / dictAccessValues.py
Created November 6, 2020 19:59
How to Access Values in a Dictionary
# YTL Currency
dict_YTL_currency = {'Pound': 0.091, 'Euro': 0.11, 'Usd': 0.12, 'Afghani': 9.15, 'Bangladeshi Taka': 10.0 }
# simple lookup with []
print(dict_YTL_currency['Pound'])
# simple lookup with [] when the key does not exist
print(dict_YTL_currency['Dinar']) # trows KeyError
# avoiding KeyError with get() method
@eisbilen
eisbilen / dictCompConditionals.py
Created November 3, 2020 22:16
Adding Conditionals to Dictionary Comprehension
# YTL Currency
dict_YTL_currency = {'Pound': 0.091, 'Euro': 0.11, 'Usd': 0.12, 'Afghani': 9.15, 'Bangladeshi Taka': 10.0 }
# Adding If Conditions
dict_YTL_strong = {k:v for (k,v) in dict_YTL_currency.items() if v>1}
dict_YTL_weak = {k:v for (k,v) in dict_YTL_currency.items() if v<1}
print(dict_YTL_strong)
print(dict_YTL_weak)
# Adding Multiple If Conditions
@eisbilen
eisbilen / dictComprehensions.py
Last active November 3, 2020 21:30
Dictionary Comprehensions
# GBP to YTL conversion
GBP_to_YTL = 11.0
# item price in GBP
dictionary_GBP = {'milk': 1.02, 'coffee': 2.5, 'bread': 1.5}
# creating a new dictionary with dictionary comprehension
dictionary_YTL = {key: value*GBP_to_YTL for (key, value) in dictionary_GBP.items()}
print(dictionary_YTL)
# output
@eisbilen
eisbilen / dictWithfromkeys.py
Created November 2, 2020 20:43
New Dictionary With fromkeys() Method
# fromkeys() method returns a dictionary with the specified keys and the specified value.
# iterable specifying the keys of the new dictionary
keys = ["IST", "SAW", "STN"]
# the value for all keys as an optional parameter
# default value is None
value ="to_be_defined"
dictionary = dict.fromkeys(keys, value)
@eisbilen
eisbilen / dictWithZip.py
Created November 2, 2020 20:24
Dictionary With Zip Function
# zip() function make an iterator that aggregates elements from each of the iterables
# we can create a dictionary when it is used in dict() method
# keys/values are available as a list
keys = ["IST", "SAW", "STN"]
values = ["Istanbul Airport", "Sabiha Gokcen International Airport", "London Stansted Airport"]
dictionary = dict(zip(keys, values))
print(dictionary)