Dictionaries in Python
By: Khilari Pradip J
Tags: Dictionaries, All About Dictionary, Dictionaries In Python, Python, Python Programming

Dictionary is one of the data type in python. It uses to connect pieces of related information. It can store limitless amount of information. It is an unordered collection of data values, elements of dictionary does not have any order, since you cannot refer to an element by using an index.

A Dictionary in Python is a collection of key-value pair. Each key is connected to value associated with that key. Value in dictionary can be of any data type, i.e. it can be number, a string, a list or even another dictionary, it can be duplicate. Whereas key cannot be repeated and must be immutable.

Dictionary create by placing sequence of elements in curly ‘{}’ braces and separated by ‘comma’. It has pair of values, one is key and another associated element is value.

Each key is separate its value by a colon (:) and pair of values are separated by comma.

Syntax:

dictionary_name={‘key1’:’value1’, ‘key2’:’value2’,’key3’:’value3’ ……….}

Here, one key can have more than one value also.

Example. - car={‘model’:’swift’, ’brand’:’Maruti-Suzuki’, ‘year’:2020,}

Note- Putting comma after last element is good practice.

· How to access value from dictionary

e.g. car={‘model’:’swift’,’color’:’Red’,}

print(car[‘color’])

This gives the value corresponds with the key ‘color’ from dictionary car i.e. ‘Red’.

· Adding new key value pair with existing dictionary.

You can add new key value pair to dictionary at any time in program

e.g. car[‘type’]=’SUV’

This will add as a new key-value pair in above example.

· Empty Dictionary

We can start with an empty dictionary and we can add each new element to it

e.g.- car={}

car[‘color’]=’Orange’

car[‘brand’]=’Ford’

This adds new key-value pair to the empty dictionary.

· Change values in a Dictionary

If you want to change or modify in given dictionary, give dictionary name in square bracket and values corresponding to key

e.g.- car[‘color’]=’blue’

This changes ‘Orange ‘ to ‘Blue’ in above dictionary

· Deleting ‘key-value’ pairs

To delete element from the dictionary use ‘del’ keyword.

e.g. del car[‘brand’]

This completely delete the key-value pair from the dictionary

· Pop() Method

Pop() method is used to delete the value of the key specified

e.g.- car.pop(‘brand’)

Here ‘brand’ is specified key and it delete ‘brand’ associated with its value.

· Popitem() method

Popitem() method is used to remove arbitrary element (key-value) pair from the existing dictionary

e.g.- car.popitem()

· Delete all elements from the dictionary

Clear() method is use to delete all elements

e.g.- car.clear()

This delete all data from dictionary and make it empty.

· Looping through a dictionary

In python dictionary can have few or millions of key-value pairs. It can contain large amount of data, we can loop through the dictionary.

· Looping all key-values

e.g.- person={‘first_name’:’John’,’last_name’:’Abhraham’,’location’:’Mumbai’,}

If you want to access single piece of information, we can use simple method. But if we want to see all data stored in dictionary, we need to loop through the dictionary

e.g.-for key, value in person.items():

print(‘

key :’+key)

print(‘

value :’+value)

Note-Here instead of key and value we can use any variable name

· Visiting all Keys in dictionary

Key() method is use to loop through all keys in dictionary, when you don’t need the values

for k in person.keys():

print(k.title())

This will print all keys from dictionary person

· Visiting dictionary keys in order

In dictionary keys are not in ordered, and if we want keys by order, one way to return keys in order is sort them.

sorted() — is the method to get copy of the keys in ordered form

e.g.- for k in sorted(car.keys()):

print(k.title())

· Visiting all values in dictionary

If you want only values from the dictionary, you can loop through all values

e.g.- for v in car.values():

print(v.title())

· Nesting

If you want to store set of dictionaries in a list or list of elements as values in a dictionary, this is known as Nesting.

You can nest set of dictionaries in list, a list of items inside the dictionary or dictionary inside another dictionary.

· Dictionaries in a list

e.g.- car1={‘model’=’swift’,’brand’:’maruti-suzuki’,’color’:’white’,}

car2={‘model’=’figo’,’brand’:’Ford’,’color’:’gray’,}

car3={‘model’=’i20’,’brand’:’Hundai’,’color’:’Red’,}

These are the three dictionaries , we create a list above of dictionaries.

cars=[car1,car2,car3]

# visit the list cars elements

for c in cars:

print(c)

· List in a dictionary

We can put list inside the dictionary.

e.g.-car={‘model’:’swift’,‘color’:[‘Red’,’Blue’,’Orange’,’white’,’black’],}

#print dictionary car’s color key’s values

for c in car[‘color’]:

print(‘\t’+ c)

· Dictionaries in a dictionary

You can nest dictionary within dictionary

e.g. friends={

‘Arvind’:{‘first_name’:’Avi’,’last_name’:’Reddy’,’location’:’Hydrabad’,},

‘Abhi’:{‘first_name’:’Abhijit’,’last_name’:’Gupta’,’location’:’Delhi’,},

}

for k, v in friends.items():

print(k.title())

print(v.title())