Few mistakes when using Python dictionary
Usually, A dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. Each key-value pair in a Dictionary is separated by a ‘colon’, whereas each key is separated by a ‘comma’.
my_dict = {1: 'Geeks', 2: 'For', 3:'Geeks'} print(my_dict) |
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
We generally use dictionaries to access the items with its key value, inside square brackets.
my_dict = {1: 'Geeks', 2: 'For', 3:'Geeks'} print(my_dict[1]) print(my_dict[2]) print(my_dict[3]) |
Geeks For Geeks
The common operations of dictionaries are:
- To get the values of the dictionary we use values() method.
my_dict={1:'Geeks',2:'For',3:'Geeks'}print(my_dict.values())chevron_rightfilter_noneOutput:dict_values(['Geeks', 'For', 'Geeks'])
- To get the keys of the dictionary we use keys() method.
my_dict={1:'Geeks',2:'For',3:'Geeks'}print(my_dict.keys())chevron_rightfilter_noneOutput:dict_keys([1, 2, 3])
- To add a new entry into the dictionary
my_dict={1:'Geeks',2:'For',3:'Geeks'}my_dict[4]='Python'print(my_dict)chevron_rightfilter_noneOutput:{1: 'Geeks', 2: 'For', 3: 'Geeks', 4: 'Python'} - To change the value of the entry
my_dict={1:'Geeks',2:'For',3:'Geeks'}my_dict[3]='Python'print(my_dict)chevron_rightfilter_noneOutput:{1: 'Geeks', 2: 'For', 3: 'Python'} - To delete an entry from dictionary
my_dict={1:'Geeks',2:'For',3:'Geeks'}delmy_dict[3]print(my_dict)chevron_rightfilter_noneOutput:{1: 'Geeks', 2: 'For'} - To copy the dictionary
my_dict={1:'Geeks',2:'For',3:'Geeks'}my_dict1=my_dictprint(my_dict1)chevron_rightfilter_noneOutput:{1: 'Geeks', 2: 'For', 3: 'Geeks'} - To remove all entries.
my_dict={1:'Geeks',2:'For',3:'Geeks'}my_dict.clear()print(my_dict)chevron_rightfilter_noneOutput:{} - To find the number of entries.
my_dict={1:'Geeks',2:'For',3:'Geeks'}z=len(my_dict)print(z)chevron_rightfilter_noneOutput:3
Common mistakes while using dicts and overcomes
- To access the value of the key, we generally use dict_name[key_name] instead we should use get() method to get rid of the exceptions thrown all throughout your code.
- To update the value of the key, we generally use dict_name[key_name]=’new_value’ instead we should update(key=value) method to get rid of the exceptions thrown all throughout your code.
- To copy the dictionary, we generally use dict_name = new_dict_name instead we should use copy() method to get rid of the exceptions thrown all throughout your code.
When not to use dicts
- Dicts are useful but they’re not the only associative array structure in Python. Often there is a more specialised container type like set, tuple etc.
- Numeric values with different types can be equal (e.g. 1 == 1.0), in which case they represent the same key.
Recommended Posts:
- Python dictionary (Avoiding Mistakes)
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python Dictionary
- Python | Get the first key in dictionary
- Python | Get key from value in Dictionary
- Python | Dictionary has_key()
- Python | Add new keys to a dictionary
- Python Dictionary Comprehension
- Python Dictionary | pop() method
- Python | Key index in Dictionary
- Python Dictionary copy()
- Python | Increment value in dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



