Posts

Showing posts with the label python dictionary

Python dictionary

Dictionaries are one of the built-in data types in Python. Like the language dictionary has words and their meaning written in it, the python dictionaries have 'key' and the 'value' corresponding to the 'key' stored in it. The python dictionary can be created by writing key-value pairs inside a curly bracket {}. An empty dictionary can be initiated as follows: d = {} Also, we can directly create a dictionary with initial key-value pairs. The key- value pairs must be in the format {key: value} . Following is an example. d = {'A': 3, 'Animals': ['dog', 'lion']} Here, we added two key-value pairs, {'A': 3} and { 'Animals': ['dog', 'lion']}. Note that the values can be a number, a string, a list or even a dictionary. They keys of the dictionary, however, should be an immutable objects such as strings, numbers, and tuples. Add new item to a dictionary New items in a dictionary can be added t...