#1. How to initilize a Dictionary in python.
Add key:value pair in a Dictonary using List comprehension method.
# Declare variable `houses_rowwise`
houses_rowwise = [
{
"price_approx_usd": 115910.26,
"surface_covered_in_m2": 128,
"rooms": 4,
},
{
"price_approx_usd": 48718.17,
"surface_covered_in_m2": 210,
"rooms": 3,
},
]
#2. How to Add elements in Dictionary Dynamically.
Solution A: Left side variable of assignment operator '=' representing as Key and right side expression as value.
# Add key:value pair using List comprehension
[ i.update(price_per_m2_New =i["price_approx_usd"] / i["surface_covered_in_m2"]) for i in houses_rowwise ]
houses_rowwise
Solution B: Adding key:value pair using a for loop approach
for i in houses_rowwise:
i["price_per_m2"] = i["price_approx_usd"] / i["surface_covered_in_m2"]
#3. How to create multiple List in Python
Solution: Multiple List in Python
d = [[{"a":5, "b":10}, {"a":15, "b":20}], [{"a":8, "b":18}, {"a":8, "b":18}]]
for i in d:
print(i)
print("\nEquivalent to:")
[ print(i) for i in d]
#3. How to iterate over multiple lists in Python
Solution: Multiple List access in Python
d = [[{"a":5, "b":10}, {"a":15, "b":20}], [{"a":8, "b":18}, {"a":8, "b":18}]]
for i in d:
for j in i:
print(j)
print("\nEquivalent to:")
[ print(y) for x in d for y in x ]
#4. How to access Dictionary items from nested lists in Python
Solution: Read Dictionary items from chain-lists
d = [[{"a":5, "b":10}, {"a":15, "b":20}], [{"a":8, "b":18}, {"a":8, "b":18}]]
for x in d:
for y in x:
for z in y:
print(z)
print()
print("Using Dictionary Chain Rule:")
res = [ print(z) for x in d for y in x for z in y]
del(res) # List object is not required, hence, Deleted.
#5. How to change a datatype of the elemtent using list comprehension chain rule in python
list_of_lists = [['4', '8'], ['4', '2', '28'], ['1', '12'], ['3', '6', '2']]
res1 = [int(i) for sublist in list_of_lists for i in sublist]
print(res1)
#[4, 8, 4, 2, 28, 1, 12, 3, 6, 2]
res2 = [[int(j) for j in i] for i in list_of_lists]
print(res2)
#[[4, 8], [4, 2, 28], [1, 12], [3, 6, 2]]
#6. How to assign a variable in list comprehension python
d = [{"a":5, "b":10}, {"a":10, "b":20}, {"a":20, "b":18}, {"a":30, "b":18}]
k = 0
p = 0
for i in d:
k = k + (i["a"])
print(k/len(d))
# This symbol ":=" is used to assign a value in List Comprehension as it does not support normal assignment.
total_sum = [ p := p+ i["a"] for i in d][-1]
print(total_sum/len(d))
#7. How to Create a Dictionary using List.
Solution: The zip() function helps to join two different list as dictionary.
# Definition of countries and capital
countries = ['spain', 'france', 'germany', 'norway']
capitals = ['madrid', 'paris', 'berlin', 'oslo']
# From string in countries and capitals, create dictionary europe
europe = dict(zip(countries,capitals))
# Print europe
print(europe)
#8. How to add a new element in the Dictionary
europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }
# Add italy to europe
europe["italy"] = "rome"
# Print out italy in europe
print("italy" in europe)
#9. How to access key's value from nested Dictionary
# Dictionary of dictionaries
europe = { 'spain': { 'capital':'madrid', 'population':46.77 },
'france': { 'capital':'paris', 'population':66.03 },
'germany': { 'capital':'berlin', 'population':80.62 },
'norway': { 'capital':'oslo', 'population':5.084 } }
# Print out the capital of France
print(europe["france"]["capital"])
# Create sub-dictionary data
data = {"capital":"rome" , "population":59.83}
# Add data to europe under key 'italy'
europe["italy"]= data
# Print europe
print(europe)