Python Dictionary (With Examples)

Python dictionary is an unordered collection of elements that are changeable and indexed. Dictionaries are written with curly braces, and they have keys and values. While other compound data types like lists or tuples have only value as an item, a dictionary has a key-value pair.

How to create a dictionary in Python

To create a dictionary in Python, place elements inside curly braces {} separated by a comma. An element has a key; the corresponding value is expressed as a key-value pair.

dictA = {'name': 'Krunal', 'age': 26}

print(dictA)

Output

Python Dictionary Example Tutorial | Data Structures In Python

The above is the simplest way to create a dictionary.

In Dictionary, the values can be of any data type and can repeat, but the keys must be immutable and unique. Therefore, dictionaries must have unique keys.

We can also use the dict() method to create a dictionary.

dictB = dict({1:'Django', 2:'Flask'})

print(dictB)

Output

How to create a dictionary

How to access elements in Dictionary

To access elements in the Python dictionary, refer to their key name inside square brackets.

dictB = dict({1:'Django', 2:'Flask'})

print(dictB[1])

Output

Accessing Items in Dictionary

You can also use the get () method, which will give you the same result.

dictB = dict({1:'Django', 2:'Flask'})

print(dictB.get(1))

How to modify in Dictionary in Python

To change a dictionary in Python, use an assignment operator.

A dictionary is mutable; that is why we can add new items or change the value of an existing object. In addition, you can change the value of the specific element by referring to its key name.

The value gets updated if the key is present; a new key: value pair is added to the Dictionary.

dictC = {'phpf1': 'Laravel', 'phpf2':'Symfony', 'phpf3':'Zend'}

print(dictC)

dictC['phpf3'] = 'Yii'

print('After changing values: ')

print(dictC)

Output

Modify Values in Dictionary

Add the item to the Dictionary.

dictC = {'phpf1': 'Laravel', 'phpf2':'Symfony', 'phpf3':'Zend'}

print(dictC)

dictC['phpf4'] = 'Yii'

print('After adding values: ')

print(dictC)

Output

Add the item to the Dictionary.

How to remove an element from a dictionary

To remove an element from a dictionary in Python, you can use the “dict.pop()” method. The pop() method removes an item with the provided key and returns the value.

dictD = {1:18, 2:19, 3:20, 4:21}

poppedE = dictD.pop(3)

print('Popped Item: ',poppedE)

print(dictD)

Output

Delete or remove elements from a Dictionary

How to loop through a Dictionary in Python

To loop through a dictionary in Python, use the for loop. The for loop is used to iterate each element of the Dictionary. When looping through the Dictionary, the return value is the key to the Dictionary, but we can also return the values.

dictK = {'c1': 'Ben 10', 'c2': 'Kung Fu Panda', 'c3': 'Scooby Doo'}

for key in dictK:
  print(dictK[key])

Output

Loop Through a Dictionary

We can also print the keys.

dictK = {'c1': 'Ben 10', 'c2': 'Kung Fu Panda', 'c3': 'Scooby Doo'}

for key in dictK:
  print(key)

Dictionary methods

Method Description
dict.clear() It removes all the elements from the dictionary
dict.copy() It returns a copy of the dictionary
dict.get() It returns the value of specified key
dict.items() It returns a list containing a tuple for each key-value pair
dict.keys() It returns a list containing the dictionary’s keys
dict.update() It updates the dictionary with specified key-value pairs
dict.values() It returns a list of all the values of the dictionary
dict.pop() It remove the element with the specified key
dict.popItem() It removes the last inserted key-value pair
 dict.setdefault() It sets the key to the default value if the key is not specified in the dictionary

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.