Menu Close

Python Dictionary Tutorial

Python dictionary tutorial

In this tutorial, we are going to learn all about Python dictionaries along with their important methods. Dictionary in python is used to store data in the form of key: value pairs.

In the previous tutorial, we have seen all about Python list and sets and as well as methods of them.
In this guide, we will go through the python dictionary and its some python dictionary methods which make it very easy to work with a dictionary in Python.

What is Python Dictionary?

Dictionary in python is a data structure that stores the items in the form of key: value pairs.

Python dictionary is a collection of elements that are unordered, indexed, changeable or mutable ( Dictionary values can be changed ).

Dictionary in python does not allow duplicate value. Dictionary check duplicate value on the basis of the key.

Dictionary is written in curly bracket ( {} ). Python dictionary is a different thing from other collection data types like list, tuple, and set because dictionary store information in the form of a key: value pairs that mean in a dictionary each value has their corresponding key.
Dictionary values can be accessed by using their keys.

Create Dictionary in Python

To create dictionary in python use curly bracket ( {} ).

myDict= 
{ 

  "id":12,
  "first_name":"Maria",
  "last_name":"Alex",
  "city":"New Yark",
  "salary":"$1000"
        
}
print(myDict)

Output

{'id': 12, 'first_name': 'Maria', 'last_name': 'Alex', 'city': 'New Yark', 'salary': '$1000'}

Create Python Dictionary using dict() constructor

Python provides the best built-in function to create a dictionary, which is called dict(). dict() function accepts an arbitrary number of keyword arguments ( **kwargs ).

my_dict = dict(name='Vishvajit', course = 'BCA', address = 'Noida', occupation = 'Python programmer')
print(type(my_dict))
print(my_dict)
print(my_dict['name'])

Output

<class 'dict'>
{'name': 'Vishvajit', 'course': 'BCA', 'address': 'Noida', 'occupation': 'Python programmer'}
Vishvajit

Type of Dictionary

To find the data type of Python Dictionary use type() built-in function.

print(type(myDict))

Output will be:- <class ‘dict’>

Access item from Python Dictionary

To access item from dictionary you can use key name inside square bracket ( [] ).

myDict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"
		
}
print(myDict['first_name'])

Output will be:- Maria

Python dictionary provide another method get() to access item from dictionary.

myDict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"
		
}
print(myDict.get("salary"))

Output will be:- $1000

Loop Through Python Dictionary

To loop through a dictionary, use for loop. You have to remember some points if you using for loop in a python dictionary.

  • If you use for loop for iterate the dictionary, The return value is the key of dictionary.
myDict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"
		
}
for x in myDict:
	print(x)

Output

id
first_name
last_name
city
salary
  • If you want to display a value name instead of a key name, then you need to refer to the key name inside the square bracket.
myDict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"
		
}
for x in myDict:
	print(myDict[x])

Output

12
Maria
Alex
New Yark
$1000
  • To display the keys of dictionary use keys() function.
myDict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"
		
}
for x in myDict.keys():
	print(x)

Output

id
first_name
last_name
city
salary
  • To display the values of dictionary use values() function.
myDict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"
		
}
for x in myDict.values():
	print(x)

Output

12
Maria
Alex
New Yark
$1000
  • To display the both keys and values from dictionary use items() function.
myDict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"
		
}
for x, y in myDict.items():
	print(f"{x} = {y}")

Output

id = 12
first_name = Maria
last_name = Alex
city = New Yark
salary = $1000

Change item in Python Dictionary

Dictionary values can be changed using the key name and by assigning a new value to that key.

myDict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"
		
}

myDict['first_name'] = 'John'
print(myDict)

Output

{'id': 12, 'first_name': 'John', 'last_name': 'Alex', 'city': 'New Yark', 'salary': '$1000'}

Get python dictionary length

To get the length of python dictionary use len() function.

print(len(myDict))

Output will be:- 5

Remove item in a dictionary

  • To remove items from the dictionary for the specific key name, use the python dictionary pop() method.
my_Dict= {

"id":10,
"first_name":"Jasmin",
"last_name":"Joy",
"city":"California",
"salary":"$1200"
	
}
	
my_Dict.pop('salary')
print(my_Dict)

Output

{'id': 10, 'first_name': 'Jasmin', 'last_name': 'Joy', 'city': 'California'}
  • To remove the last item from the dictionary, use python dictionary popitem() methods.
my_Dict= {

"id":10,
"first_name":"Jasmin",
"last_name":"Joy",
"city":"California",
"salary":"$1200"
	
}
	
my_Dict.popitem()
print(my_Dict)

Output

{'id': 10, 'first_name': 'Jasmin', 'last_name': 'Joy', 'city': 'California'}
  • To remove item from dictionary use del keyword.
del my_Dict['salary']
print(my_Dict)

Output

{'id': 10, 'first_name': 'Jasmin', 'last_name': 'Joy', 'city': 'California'}
  • To clear the dictionary in python, use dictionary clear() method.
myDict.clear()
print(myDict)

Output will be:- {}

Copy Dictionary

  • Sometimes you need to copy of dictionary into another variable then you can use the Python dictionary built-in method copy().
myDict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"		
}
result = myDict.copy()
print(result)

Output

{'id': 12, 'first_name': 'Maria', 'last_name': 'Alex', 'city': 'New Yark', 'salary': '$1000'}
  • You have another method dict() is available to copy of python dictionary.
my_Dict= { 	

	"id":12,
	"first_name":"Maria",
	"last_name":"Alex",
	"city":"New Yark",
	"salary":"$1000"		
}
x = dict(my_Dict)
print(x)

Output

{'id': 12, 'first_name': 'Maria', 'last_name': 'Alex', 'city': 'New Yark', 'salary': '$1000'}

Nested Dictionary

Python dictionary provides the best facility is a nested dictionary that means you can create a dictionary inside another dictionary.

Employees = {

"emp1": {
"name":"Ajhar",
"age":23,
"salary":12000
},

"emp2":{
"name":"sara",
"age":25,
"salary":20000
},

"emp3":{
"name":"Maria",
"age":24,
"salary":40000
}

}

print(Employees)

Output

{'emp1': {'name': 'Ajhar', 'age': 23, 'salary': 12000}, 'emp2': {'name': 'sara', 'age': 25, 'salary': 20000}, 'emp3': {'name': 'Maria', 'age': 24, 'salary': 40000}}

Dictionary methods

Conclusion

In this Python dictionary tutorial, you have learned all about the dictionary and its some important methods.
Python is an unordered collection of data values, used to store data in the form of key: value paired like a map. In a Python dictionary, each key has its own element.

I hope, you don’t have any confusion regarding Python dictionary. To learn about any particular dictionary method, you can click above assigned dictionary methods.

If you like this tutorial, please keep visiting for further tutorial.

For more information:- Click Here

Have a nice day!

Python String Tutorial
Python Tuple Tutorial

Was this post helpful?

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *