AppDividend
Latest Code Tutorials

Python Dictionary Example Tutorial | Data Structures In Python

0

Python Dictionary Example Tutorial | Data Structures In Python is today’s topic. In Python, A dictionary is the collection which is unordered, changeable and indexed. Dictionaries are written with the curly braces, and they have keys and values. A Python dictionary is an unordered collection of elements. While other compound data types like list or tuple have only value as an item, a dictionary has a key-value pair.

How to create a dictionary

You can create a simple Python Dictionary by placing items inside curly braces {} separated by a comma. An element has a key and the corresponding value expressed as a key-value pair.

Python Dictionary Example Tutorial

Let us create a simple Dictionary in Python.

# app.py

dictA = {'name': 'Krunal', 'age': 26}
print(dictA)

 

Python Dictionary Example Tutorial | Data Structures In Python

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 of an immutable type and must be unique. Dictionary must have unique keys.

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

# app.py

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

 

How to create a dictionary

Accessing Items in Dictionary

We can access the items by referring to its key name inside square brackets. See the below example.

# app.py

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

See the output below.

 

Accessing Items in Dictionary

You can also use the method called get() that will give you the same result.

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

Modify Values in Dictionary

Dictionary is mutable that is why we can add new items or change the value of an existing object using an assignment operator. You can change the value of the specific element by referring to its key name.

If the key is already present, the value gets updated, else a new key: value pair is added to the dictionary.

Let’s see the below example.

# app.py

dictC = {'phpf1': 'Laravel', 'phpf2':'Symfony', 'phpf3':'Zend'}
print(dictC)
dictC['phpf3'] = 'Yii'
print('After changing values: ')
print(dictC)

See the output below.

 

Modify Values in Dictionary

Add the item to the Dictionary.

# app.py

dictC = {'phpf1': 'Laravel', 'phpf2':'Symfony', 'phpf3':'Zend'}
print(dictC)
dictC['phpf4'] = 'Yii'
print('After adding values: ')
print(dictC)

See the output.

 

Related Posts
1 of 42

Add the item to the Dictionary.

Delete or remove an item from a Dictionary

We can remove a particular element in a dictionary by using the method pop(). The pop() method removes an item with the provided key and returns the value. See the below example.

# app.py

dictD = {1:18, 2:19, 3:20, 4:21}
poppedE = dictD.pop(3)
print('Popped Item: ',poppedE)
print(dictD)

 

Delete or remove elements from a Dictionary

Loop Through a Dictionary

You can use for loop to iterate each item of the Dictionary. When looping through the dictionary, the return value is the key of the dictionary, but we can also return the values.

Let us print the values of the dictionary.

# app.py

dictK = {'c1': 'Ben 10', 'c2': 'Kung Fu Panda', 'c3': 'Scooby Doo'}
for key in dictK:
    print(dictK[key])

 

Loop Through a Dictionary

We can also print the keys. See the below code.

# app.py

dictK = {'c1': 'Ben 10', 'c2': 'Kung Fu Panda', 'c3': 'Scooby Doo'}
for key in dictK:
    print(key)

Dictionary Length

We can use the len() method to determine the length of the dictionary.

# app.py

dictC = {'phpf1': 'Laravel', 'phpf2':'Symfony', 'phpf3':'Zend'}
print(len(dictC))

 

Dictionary Length in Python

So, in this Python Dictionary Example Tutorial, we have covered almost all topic regarding Dictionary Data Structure in Python.

Finally, Python Dictionary Example Tutorial is over. Thanks for taking.

Leave A Reply

Your email address will not be published.

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