Python Dictionary Data Type
 

Python Dictionary Data Type

 

Python dictionary is an unordered collection of data values it stores data in key:value pairs. A pair of braces i.e. {} without any value creates an empty dictionary. You can initialize it by providing key and value pairs separated with a comma.

In this article, you will learn the Python dictionary and different operations that can be performed on it.

How to create Python dictionaries

A Python dictionary can be created in two ways first is that you place the comma-separated items inside curly braces and the second is to create using dict() function.

For example –

# Create an empty dictionary
user0={}
# Create dictionary with first method
user1={'name' : 'Vinay', 'age' : 22}
# Create dictionary using dict() function
user2= dict(name= 'Sumit', age=24)
# Now dispaly and check their data type
print(user0)
print(type(user0))
print(user1)
print(type(user1))
print(user2)
print(type(user2))

You can see the output in the given image –

Use the following syntax if the dictionary has too many items this will make code more readable –

user3={
'name':'Ankit',
'age': 25,
'place':'Lucknow',
'country':'India'
}

Accessing data from Python dictionary

A Python dictionary is an unordered collection of data so there is no indexing using which we can access the dictionary elements. Instead of using indexing, we use the key to access values corresponding to it.

You can use keys inside square brackets or get() method to access a value.

For example –

user1={'name' : 'Vinay', 'age' : 22}
print(user1[name])
# OR use get() method to access values
print(user.get(age))

Now see the output of this program in the given image –

Adding data to a dictionary

Dictionaries are mutable which means we can add, remove or update the elements of a Python dictionary. Let us have the following dictionary –

user3={
'name':'Ankit',
'age': 25,
'place':'Lucknow',
'country':'India'
}

To update the place from Lucknow to Delhi you can use –

user3['place'] = 'Delhi'

OR to add an item let’s say pin we will use the following statement –

user3['pin code']= 110001

Now display the update dictionary by using –

print(user3)

You can see the updated dictionary in the given image –

Removing Python dictionary elements

To remove an element from a dictionary you can use the pop() method. The pop() method takes the key as an argument.

user3={
'name':'Ankit',
'age': 25,
'place':'Lucknow',
'country':'India'
}

In the above dictionary to remove place, we will use the following statement –

print(user3.pop('place'))

Similarly, the popitem() method randomly deletes an element or key-value pair from the dictionary.

To remove all the items and print the dictionary use –

user3.clear()
print(user3)

You can delete a dictionary itself by using the del keyword-

del user3

Now see the output in the given image –

Looping in dictionary

There are different ways in which you can iterate over a dictionary.

  • Iterate through all keys
  • Iterate through all values
  • Iterate through all keys, value pairs

Lets we have the following dictionary –

user3={
'name':'Ankit',
'age': 25,
'place':'Lucknow',
'country':'India'
}

Now you can iterate and print all the keys of the user3 dictionary

for i in user3:
     print(i)

You can see the output in the given image –

To print all the values we will use the values() method as it is given below –

for i in user3.values():
        print (i)

If you run the program it will display all the values of the dictionary user3 –

By using the items() method you can print all key-value pairs of a dictionary.

for i in user3.items():
       print (i)

You can see the output in the given image-

Python dictionary methods

There are some methods that can be used with dictionaries in Python. We have already used some of them now see the complete list of these methods below.

MethodDescription
clear()Removes all the elements from a dictionary
copy()Returns a copy of dictionary
get()Returns the value of key that you specified
fromkey(keys, v)Returns a new dictionary with key from keys and value equal to v
items()Returns tuples for each key value pair of dictionary
keys()Returns a list that contains dictionary keys
pop()Removes the specified item
popitem()Remove and returns an arbitrary dictionary item
setdefault()Returns the value of the specified key. If the key does not exist it inserts key with a value to the dictionary.
update()Update the dictionary with key value pair
values()Returns all the values of dictionary

Ok, that’s all for now. Now if you have a query please write us in the comments below.

Previous Python Tuple Data Type
Next Writing the First Hello World Program in C
 

About author

You might also like

Programming 0 Comments

Setup Python Development Environment With VS Code In Linux

Visual studio code (VS Code) is a free and open-source IDE that is developed by Microsoft. It is a lightweight IDE that has powerful features like smart code completion, code

Python Programming 0 Comments

Data Types in Python

This post was last updated on February 2nd, 2021 at 07:54 pmIn programming, the data type is the classification of data items. In Python, it tells the interpreter how the

Programming 0 Comments

Type Conversion in Python

The data type is the classification of data items it tells the interpreter how a user is intended to use the data. Python has various data types and a variable

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply