Dictionary in Python

Dictionary in Python is similar to an associative array in PHP.  Dictionary are create using curly braces {} inside it the key is separated with items using a colon (:). For accessing items of a dictionary, the key is been for this. Dictionary is mutable
Dictionary in Python


Contents

  1. Creating a Dictionary
  2. Accessing Dictionary
  3. Update Dictionary
  4. Delete Dictionary
  5. Dictionary Functions

 


Creating a Dictionary

Dictionary is created using curly braces {} inside it items are stored with a key. Key and item and separated using a colon (:) and items are separated by a comma (,).

Keys should be unique within the dictionary if the same key is reused multiple times in a dictionary then its takes last defined key with the item into the dictionary and remove other. In dictionary tuple can also be used as key in dictionary

Syntax –

dict-variablename = {key1:value1,key2:value2,key3:value3,...}

Example –

dict1 = {'Sonarika':23,'Vishal':24,'Yogesh':22}
dict2 = {('Mayank'):23,'Sunil':22}

print("dict1 : ", dict1)
print("dict2 : ", dict2)

When we execute the above program, it will produce the following output –

dict1 : {'Sonarika':23,'Vishal':24,'Yogesh':22}
dict2 : {('Mayank'):23,'Sunil':22}

Accessing Dictionary

For accessing items from a dictionary, use square brackets [] within it enters a key name.

Syntax –

dict-variablename[key]

Example –

dict1 = {'Sonarika':23,'Vishal':24,'Yogesh':22}
dict2 = {('Mayank'):23,'Sunil':22}

print("dict1['Vishal'] : ", dict1['Vishal'])
print("dict2[('Mayank')] : ", dict2[('Mayank')])

When we execute the above program, it will produce the following output –

dict1['Vishal'] : 24
dict2[('Mayank')] : 23

Update Dictionary

On the basis of the key, we can update existing dictionary.

Syntax –

dict-variablename[key] = value

Example –

dict1 = {'Sonarika':23,'Vishal':24,'Yogesh':22}
dict2 = {('Mayank'):23,'Sunil':22}

dict1['Vishal'] = 25
dict2['Sunil'] = 24
print("dict1 : ", dict1)
print("dict2 : ", dict2)

When we execute the above program, it will produce the following output –

dict1 : {'Sonarika':23,'Vishal':25,'Yogesh':22}
dict2 : {('Mayank'):23,'Sunil':24}

Delete Dictionary

For deleting a dictionary or dictionary item, we use del statement.

Syntax –

del dict-variablename
del dict-variablename[key]

Example –

dict1 = {'Sonarika':23,'Vishal':24,'Yogesh':22}
dict2 = {('Mayank'):23,'Sunil':22}

del dict1
del dict2['Sunil']

 


Dictionary Functions

1. clear – Remove all elements of a dictionary

Syntax – clear()

Example –

dict1 = {'Sonarika':23,'Vishal':24,'Yogesh':22}
print("dict1 : ",dict1.clear(dict1))

Output

dict1 : None

2. copy – Return a copy of a dictionary.

Syntax – copy()

Example –

dict1 = {'Sonarika':23,'Vishal':24,'Yogesh':22}
dict2 = dict1.copy()
print("dict1 : ",dict1)
print("dict2 : ",dict2)

Output

dict1 : {'Sonarika':23,'Vishal':24,'Yogesh':22}
dict2 : {'Sonarika':23,'Vishal':24,'Yogesh':22}

 

3. fromkeys – Create a new dictionary from sequence and value is optional default value is None. If a value is not set then it assigns None to all keys otherwise set defined value.

Syntax – fromkeys(sequence[,value])

Example –

list1 = ['Sonarika','Vishal','Yogesh']
tuple1 = ('Amit','Sunil','Mayank')

dict1 = dict.fromkeys(list1)
dict2 = dict.fromkeys(tuple1,22)
print("dict1 : ",dict1)
print("dict2 : ",dict2)

Output

dict1 : {'Sonarika': None, 'Vishal': None, 'Yogesh': None}
dict2 : {'Amit': 22, 'Sunil': 22, 'Mayank': 22}

4. get – Return item based on a key in a dictionary. If a key is not available in a dictionary then return default set value, if a default is not defined then return None.

Syntax – get(key[,default]])

Example –

dict1 = {'Sonarika':23,'Vishal':24,'Yogesh':22}

print(dict1.get('Vishal'))
print(dict1.get('Anil','Not found'))

Output

24
Not found

5. items – Return list of a dictionary in which key and values in tuple pair.

Syntax – items()

Example –

dict1 = {'Sonarika': 23, 'Vishal': 24, 'Yogesh': 22}

list1 = list(dict1.items())
print(list1)

Output

[('Yogesh', 22), ('Sonarika', 23), ('Vishal', 24)]

6. keys – Return list of keys in a dictionary.

Syntax – keys()

Example –

dict1 = {'Sonarika': 23, 'Vishal': 24, 'Yogesh': 22}

list1 = list(dict1.keys())
print(list1)

Output

['Yogesh','Sonarika','Vishal']

7. pop – Return  and remove an item from a dictionary on the basis of a key.

Syntax – pop(key)

Example –

dict1 = {'Sonarika': 23, 'Vishal': 24, 'Yogesh': 22}

print(dict1.pop('Vishal'))
print(dict1)

Output

24
{'Sonarika': 23, 'Yogesh': 22}

8. popitem – Remove and return a removing item from a dictionary. It remove a random item from a dictionary and not takes any argument.

Syntax – popitem()

Example –

dict1 = {'Sonarika': 23, 'Vishal': 24, 'Yogesh': 22}

print(dict1.popitem)
print(dict1)

Output

{'Yogesh':22}
{'Sonarika': 23, 'Vishal': 24}

9. setdefault – Return value of a given key. if a key is not defined then create a new item in a dictionary and assign the default value.

Syntax – setdefault(key[,default])

Example –

dict1 = {'Sonarika': 23, 'Vishal': 24, 'Yogesh': 22}

print(dict1.setdefault('Yogesh'))
print(dict1.setdefault('Anil',24))
print(dict1)

Output

22
24
{'Yogesh': 22, 'Sonarika': 23, 'Anil': 24, 'Vishal': 24}

10. update – Add a dictionary to another dictionary and doesn’t return any value.

Syntax – update(dictionary)

Example –

dict1 = {'Sonarika': 23, 'Vishal': 24, 'Yogesh': 22}
dict2 = {'Anil': 24, 'Sunil': 23}

dict1.update(dict2)
print(dict1)
print(dict2)

Output

{'Yogesh': 22, 'Sunil': 23, 'Sonarika': 23, 'Anil': 24, 'Vishal': 24}
{'Sunil': 23, 'Anil': 24}

11. values – Return list of values of a dictionary.

Syntax – values()

Example –

dict1 = {'Sonarika': 23, 'Vishal': 24, 'Yogesh': 22}

list1 = list(dict1.values(dict1))
print(list1)

Output

[22,23,24]

Leave a Comment