Working with Dictionary in Python
In this tutorial of python dictionary and dictionary methods with examples, You’ll learn to create a dictionary and also other operations with an example of a program.
Dictionaries are associative arrays which store values in the form of key and value pair. Dictionaries are completely different from list and tuples as they are automatically indexed and only values are provided by the user. But in the python dictionary, you have both key and value pairs. In short, you can modify the dictionary’s key and value.
Post Update Timeline
Updated on: 24-07-2020
Added new additional conntent with examples to make understanding easier.
Creating empty Dictionary Object
In short, You can use shorthand empty {} brackets or otherwise use dict() method for creation.
d = {} di = dict() print(d) print(di) #PYTHON OUTPUT {} {}
As a result, above both ways will give you an empty dictionary object.
Add values into the dictionary
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } di = dict( user_id=2, username='Sushant', age=29, salary=105000.50 ) print('Dictonary using curly brackets') print(d) print('\n') #next line print('Dictonary using dict constructor') print(di) #PYTHON OUTPUT Dictonary using curly brackets {'user_id': 1, 'username': 'Jake', 'age': 25, 'salary': 95000.75} Dictonary using dict constructor {'user_id': 2, 'username': 'Sushant', 'age': 29, 'salary': 105000.5}
Accessing Dictionary Values
Consequently, values from the dictionary can be accessed using keys or by get() method.
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } print(d['username']) print(d.get('username')) #PYTHON OUTPUT Jake Jake
By the way, the method get(key, default_value) is a python dictionary method which takes two arguments.
Update dictionary item values
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } d['username']='Rohit' print(d['username']) #PYTHON OUTPUT Rohit
Generally, the dictionary item is updated by assigning value to the item. As a result, appending a new item in a dictionary can be achieved by simply giving the name of key and value and it will be added at the bottom of the dictionary.
Syntax:
dict['key'] = 'value'
Another snippet for updating items in the dictionary.
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } d['date_of_birth'] = '1990-03-22' print(d) #PYTHON OUTPUT {'user_id': 1, 'username': 'Jake', 'age': 25, 'salary': 95000.75, 'date_of_birth': '1990-03-22'}
Python Dictionary Methods Examples
Remove or Delete item from a dictionary
In brief, the below methods are used for removing an item from a dictionary.
- Using del keyword by calling dictionary with a key name to delete. This will delete the item completely.
- Using pop() method by specifying key names inside parentheses.
Dictionary pop() method
Syntax
dict.pop('key_name')
As an example, shown above pop() will remove a single item from the dictionary. However, you can use the popitem() method to remove the last item.
Syntax of popitem()
dict.popitem()
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } del d['username'] #key 'username' will be removed from dictonary print(d) #PYTHON OUTPUT {'user_id': 1, 'age': 25, 'salary': 95000.75}
As an example, the item username is removed using the del keyword.
Remove item using the pop() method.
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } d.pop('username') print(d) #PYTHON OUTPUT {'user_id': 1, 'age': 25, 'salary': 95000.75}
Remove item using popitem() method
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } d.popitem() #this method will remove the last item from the dictionary, in this case, it is 'salary' print(d) #PYTHON OUTPUT {'user_id': 1, 'username': 'Jake', 'age': 25}
Here this method will remove the last item from the dictionary.
Empty or Truncate Dictionary using clear() method
To empty dictonary use clear() method.
Syntax
dict.clear()
In brief, explained by example.
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } d.clear() print(d) #PYTHON OUTPUT {} #we get the empty dictionary
As an example, shown above we get an empty dictionary at the end.
Completely deleting dictionary object
For example, to delete the dictionary completely you must use del keyword followed by the dictionary object.
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } del d print(d) #PYTHON ERROR OUTPUT NameError: name 'd' is not defined #because we have deleted the dictionary
However, you cannot call that object after it has been removed.
Check if a key exists in the dictionary
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } if 'username' in d: print('Exits') else: print('Does not exits') #PYTHON OUTPUT Exits
To check if a given value exists in the dictionary use values() method.
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } if 'Jake' in d.values(): print('Exits') else: print('Does not exits') #PYTHON OUTPUT Exits
Counting items in Dictionary
However, you can use len() function to count the number of items present in the dictionary.
d = { 'user_id' : 1, 'username' : 'Jake', 'age' : 25, 'salary' : 95000.75, } print(len(d)) #PYTHON OUTPUT 4
Conclusion
In conclusion, this is the end of our post on Working with Dictionary in Python and Dictionary Methods with examples. Comment if you have any queries and we’ll reach you soon.
Recent Posts




