codingstreets
Search
Close this search box.
python-dictionary

Beginner Guide to Python Dictionary with Practical Examples

In this article you will learn about Python dictionary Methods

Python Dictionary – Before moving ahead, let’s take a look at another Python collection – Python Set 

Dictionary – It is collection of unordered, indexed and changeable elements. Dictionary used curly brackets to store key:value pair elements, separated with comma after each key:value pair.

  • Left side is defined as key and right side is value.
  • Every key and value are separated with comma sign.                  

Example – Creation of dictionary.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
print(dict1)
python-dictionary
As it is shown clearly that it returned a dictionary.

Access elements – There are two ways to access elements.

1. By referring specified key name.

Example – It returns the value of specified key.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
print(dict1['age'])                                                            
python-dictionary
As it is shown clearly that it returned value of specified key.

2. get() method – By referring specified key name.

Example – It returns the value of specified key.

dict1 = {'name' :  'Alex' , 'age' : 12 , 'add' : 'NYC'}
x = dict1.get('age')

print(x)
python-dictionary
As it is shown clearly that it returned value of specified key.

Change elements – A element of dictionary can be changed by referring its key name.

Example – It returns the change value of key name ‘age’.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
dict1['age'] = 15

print(dict1)
python-dictionary
As it is shown clearly that it returned new value of specified key.

loop through a Dictionary – It is possible to loop through dictionary using ‘for’ loop. As output it returns key names. But, also possible to return values.

Example – It returns key names of dictionary through ‘for’ loop.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
for x in dict1:
      print(x) 
python-dictionary
As it is shown clearly that by through looping it returned every key element one by one.

Example – It returns value names of dictionary key through ‘for’ loop.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
for x in dict1:
     print(dict1[x])                
python-dictionary
As it is shown clearly that by through looping it returned every value element one by one.

values() method – It returns the value of the dictionary.

Example –  It returns value names of dictionary by using value() method.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
for x in dict1.values():
     print(x)                                 
python-dictionary
As it is shown clearly that it retuned only value of every key element

items() method – By using this method we can loop through both keys and values of dictionary.

Example – It returns both keys and values by items() method.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
for x , y in dict1.items():
     print(x , y) 
python-dictionary
As it is shown clearly that it returned through looping both key and value

Check if key exists– To know particular key name is present in dictionary use ‘in’ keyword.

Example – Use ‘in’ keyword to determine particular key name.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
print('add' in dict1)                                                
python-dictionary
As it is shown clearly that it returned True as key element is present in dictionary

len() method – Use len() method to get the length of element (key-value pairs) the dictionary.

Example – Use len() method to get the length of the dictionary.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
print('The length of dictionary is: ' , len(dict1))
python-dictionary
As it is shown clearly that it returned length of dictionary

Add elements – Adding an element in dictionary can be done by referring an index key and assigning value to it.

Example – Adding an element in the dictionary.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
dict1['ID'] = 458

print(dict1) 
python-dictionary
As it is shown clearly that it returned updated dictionary with new key-value.

Remove elements – Dictionary has four methods to remove its elements (key- value pairs).

1. pop() method – It removes specified key name. By default it removes last element.

Example 12- It removes specified key name to given dictionary.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
x = dict1.pop('add')

print(x)
python-dictionary
As it is shown clearly that it returned value of specified key element.

2. del method – It removes the specified key name. By default it deletes whole dictionary.

Example 13- It deletes the specified key name.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
del dict1['name']

print(dict1)             
python-dictionary
As it is shown clearly that it returned dictionary after deleting specified key-value element.

Example – It deletes whole dictionary by default.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
del dict1

print(dict1)                               
python-dictionary
As it is shown clearly that it returned an error as dictionary was not exists anymore.

clear() method – To empty whole dictionary.

Example – Use clear() method to empty whole dictionary.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
print(dict1.clear())          
python-dictionary
As it is shown clearly that it returned None as it removed all elements from dictionary

copy() method – If you copy a dictionary simply by referring a variable name like dict = dict1 then will not copy a dictionary because dict will be only a reference of dict1. Changes made in dict1 (original dictionary) will automatically also made in dict.

Example – Use copy() method to make a copy of dictionary.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
x = dict1.copy()

print(x)
python-dictionary
As it is shown clearly that it returned a copy version of original dictionary

dict() method – It returns copy of dictionary.

Example – Use dict() method to make a copy of dictionary.

dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'}
x = dict(dict1)

print(x)
python-dictionary
As it is shown clearly that it returned a copy version of original dictionary

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on