Sunday, 27 December 2020

Python: Dictionary in a dictionary

dict_in_dict.py

country_capitals = {"India" : "New Delhi", "Bangladesh" : "Dhaka", "France" : "Paris"}
numbers = {1 : "One", 2: "Two"}

my_dictionary = {"countryCapitals": country_capitals, "numbers" : numbers}

for key in my_dictionary.keys():
	print(f"{key} -> {my_dictionary[key]}")

 

Output

$ python3 dict_in_dict.py 
countryCapitals -> {'India': 'New Delhi', 'Bangladesh': 'Dhaka', 'France': 'Paris'}
numbers -> {1: 'One', 2: 'Two'}

 

 

 

 

 

Previous                                                    Next                                                    Home

Python: List in a dictionary

In this post, I am going to explain how to add a list to the dictionary.

 

list_in_dictionary.py

dict = {1 : "One"}
even_numbers = [2, 4, 6, 8]
odd_numbers = [1, 3, 5, 7]

my_dictionary = {"dict": dict, "evenNumbers" : even_numbers, "oddNumbers" : odd_numbers}

for key in my_dictionary.keys():
	print(f"{key} -> {my_dictionary[key]}")

 

This Python snippet initializes three separate variables: a dictionary named dict with the key-value pair {1: "One"}, a list named even_numbers containing even integers [2, 4, 6, 8], and another list named odd_numbers containing odd integers [1, 3, 5, 7]. Subsequently, a new dictionary named my_dictionary is created, where each key corresponds to a different variable, linking the names 'dict', 'evenNumbers', and 'oddNumbers' to their respective values.

 

The snippet then employs a for loop to iterate through the keys of my_dictionary. Within each iteration, it prints a formatted string indicating the key and its associated value. This loop effectively showcases the contents of my_dictionary by displaying each key-value pair.

 

Output

$ python3 list_in_dictionary.py 
dict -> {1: 'One'}
evenNumbers -> [2, 4, 6, 8]
oddNumbers -> [1, 3, 5, 7]

 

 

 

 

Previous                                                    Next                                                    Home

Python: List of dictionaries

In this post, I am going to explain how to create list of dictionaries.

 

list_of_dictionary.py

dict1 = {"a" : "A", "b" : "B"}
dict2 = {1 : "One", 2 : "Two"}
country_capitals = {"India" : "New Delhi", "Bangladesh" : "Dhaka", "France" : "Paris"}

list = [dict1, dict2, country_capitals]

for item in list:
	print(f"{item}")

Output

$ python3 list_of_dictionary.py {'a': 'A', 'b': 'B'} {1: 'One', 2: 'Two'} {'India': 'New Delhi', 'Bangladesh': 'Dhaka', 'France': 'Paris'}

 

 

 


 

 

Previous                                                    Next                                                    Home

Python: Looping through all values in a dictionary

‘values’ method is used to loop through all the values in a dictionary.

>>> country_capitals = {"India" : "New Delhi", "Bangladesh" : "Dhaka", "France" : "Paris"}
>>> 
>>> for capital in country_capitals.values():
...   print(f"{capital}")
... 
New Delhi
Dhaka
Paris

 

 

Previous                                                    Next                                                    Home

Python: Looping through the dictionary in the sorted order of keys

Using ‘sorted’ and ‘keys’ methods, you can loop through the dictionary in the sorted order of dictionary keys.

>>> country_capitals = {"India" : "New Delhi", "Bangladesh" : "Dhaka", "France" : "Paris"}
>>> 
>>> for country in sorted(country_capitals):
...    print(f"{country} -> {country_capitals[country]}")
... 
Bangladesh -> Dhaka
France -> Paris
India -> New Delhi

 

 

Previous                                                    Next                                                    Home

Python: Looping over a dictionary using keys

Using keys() method

‘keys()’ method return an iterator over all the keys of a dictionary.

 

Example

for key in emps.keys():

    print(f"{key} : {emps[key]}")

>>> emps = {1: {'Hari Krishna', 'Gurram'}, 3: {'Shekkappa', 'Mohan'}, 4: {'Ranganath', 'Thippisetty'}, 5: {'Ganji', 'Sudheer'}}
>>> 
>>> for key in emps.keys():
...   print(f"{key} : {emps[key]}")
... 
1 : {'Gurram', 'Hari Krishna'}
3 : {'Mohan', 'Shekkappa'}
4 : {'Thippisetty', 'Ranganath'}
5 : {'Sudheer', 'Ganji'}

 

 

 

Previous                                                    Next                                                    Home

Python: Check list emptiness using if condition

‘if list_name’

 

Syntax

if list_name:
    # List is not empty
else:
   # List is empty

 

Example

if list:
	for elem in list:
		print(elem)
	else:
		print("List is empty\n")

 

list_emptycheck.py

# Print elements of list
def print_elements(list):
   if list:
      for elem in list:
         print(elem)
   else:
      print("List is empty\n")

primes = []
print_elements(primes)

primes.append(2)
primes.append(3)
primes.append(5)
print_elements(primes)

Output

$ python3 list_empty_check.py 
List is empty

2
3
5


 

Previous                                                    Next                                                    Home