Python dict inside list
In this post, you will learn how to insert a dictionary inside a list using the Python programming language.
Python has an in-built dictionary called dict, which we can use to create an arbitrary definition of the character string. It is a collection whose values are accessible by key. The dictionary is a mapping of keys and values. A list is a sequence of indexed and ordered values. It contains a list of any type of data object with a comma separated and enclosed within a square bracket.
Lists are ordered sets of objects, whereas dictionaries are unordered sets. Dictionaries also take up more space in memory due to the hashing process. Both, lists and dictionaries are widely used to store data in Python. During the development process, we may come to the situation where we need to insert a dictionary inside a list. Both can be nested. A list can contain a dictionary, and vice versa.
Append dictionary to list python
Here is a simple example of how to append a dict inside list. Appending a dictionary to the list appends a copy of the dictionary to the list. For this, we are using the dict.copy() method. It returns a shallow copy of the dict object without references. This method doesn't require any parameters and doesn't modify the original dictionary. The dictionary object is then appended to the list using the append() method.
a_dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
a_list = ['Black', 'Purple']
dict_copy = a_dict.copy()
a_list.append(dict_copy)
print(a_list)
Output of the above code-
['Black', 'Purple', {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}]
Accessing values of dictionaries in the list
In the given Python program, we are printing some of the values of dictionaries in the list using keys.
a_dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
a_list = ['Black', 'Purple']
dict_copy = a_dict.copy()
a_list.append(dict_copy)
# accessing dictionary values
dict_access = a_list[2]
dict_access1 = a_list[2]['Red']
dict_access2 = a_list[2]['White']
#printing dictionary values
print(dict_access)
print(dict_access1)
print(dict_access2)
Output of the above code:
{'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
Rose
Lilly
Updating values of dictionaries in list
In the given Python program, we are updating some of the values of dictionaries in a list.
a_dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
a_list = ['Black', 'Purple']
dict_copy = a_dict.copy()
a_list.append(dict_copy)
# accessing dictionary values
a_list[2]['Red'] = 'Hibiscus'
a_list[2]['White'] = 'Jasmine'
#printing values
print(a_list)
Output of the above code:
['Black', 'Purple', {'Yellow': 'Marigold', 'Red': 'Hibiscus', 'White': 'Jasmine'}]
Related Articles
Python convert list to numpy array
Reverse list in Python
Convert List to Dataframe Python
Multiply each element of a list by a number in Python
Python program to list even and odd numbers of a list
Python Numpy Array Shape
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Trigonometric functions Python Numpy
Python Pandas Dataframe to CSV
Python Pandas DataFrame
Convert list to dictionary Python
Dictionary inside list Python
Convert dictionary to dataframe Python
Python Pandas CSV to Dataframe
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module