What is a Python Dictionary + Create a dictionary in Python

By: On:

In this tutorial, we will discuss what is a python dictionary and different ways to create a dictionary in Python. We will check:

  • What is a Python dictionary
  • Create a dictionary in Python
  • Accessing elements from the dictionary
  • Create dictionary python with keys and values
  • Create dictionary python from list
  • Create dictionary python for loop
  • Create dictionary python with two lists
  • Python create a dictionary from list of keys and values
  • Python sort dictionary by value
  • Initialize empty dictionary python
  • Check if a value is in a dictionary in Python

What is a Python dictionary

Now, first, let us see what is a python dictionary, A python dictionary is an unordered collection of items, And every item is in a key-value pair.


Now, let us see how to create a dictionary in Python.

If you are new to Python check out the below python tutorials:

Create a dictionary in Python

Now, we will see how to create a dictionary in Python.

A dictionary in python is a mutable data type. It is a collection of key-value pairs separated by a colon (:), enclosed with curly braces {}. A key should be unique in a dictionary.


Example:

dict1 = {'EmpName': 'Tiyasha', 'EmpId': 2043 }
print(dict1)

After writing the above code (creating a dictionary in python), Ones you will print “ dict1” then the output will appear as a “ {‘EmpName’: ‘Tiyasha’, ‘EmpId’: 2043 } ”. You can refer to the below screenshot for accessing elements from the dictionary.

Create a dictionary in python

Accessing elements from a dictionary

To access elements from dictionary specify the key name inside the square braces [].

dict1 = {'EmpName': 'Tiyasha', 'EmpId': 2043 }
value = dict1['EmpId']
print(value)

After writing the above code (accessing elements from the dictionary), Ones you will print “ value ” then the output will appear as a “ 2043 ”. Here, EmpID value is 2043. You can refer to the below screenshot for accessing elements from the dictionary.

Accessing element from dictionary

Create dictionary python with keys and values

The dictionary in python store object as key-value pairs, it stores all the data and the object in the unordered collection.

Example:

food = {'Burger': 2, 'Sandwich': 5, 'Pizza': 10 }
print('My key-value is:', food)

After writing the above code (create dictionary python with keys and values), Ones you will print “food” then the output will appear as a “My key-value is: {‘Burger’: 2, ‘Sandwich’: 5, ‘Pizza’: 10 }”. Here, the dictionary will be created with all the keys-values. You can refer to the below screenshot for Creating a dictionary python with keys and values.

Create dictionary python with keys and values

Create dictionary python from list

For creating a dictionary from a list in Python, we need make one list with values.

Example:

food = [('Burger', 2), ('Sandwich', 5), ('Pizza', 10)]
dictionary = dict((k,v) for (k,v) in food)
print(f'dictionary: {dictionary}')

After writing the above code (create dictionary python from list), Ones you will print “{dictionary}” then the output will appear as a “dictionary: {‘Burger’: 2, ‘Sandwich’: 5, ‘Pizza’: 10 }”. Here, the dictionary will be created. You can refer to the below screenshot for Creating a dictionary python from list.

Create dictionary python from list

Create dictionary python for loop

Creating a dictionary using for loop is a very efficient way in python. Looping through dictionary will return key and value

Example:

employee = {201: 'Cyrus', 202: 'Kathilin', 203: 'Mishal'}
for r in employee:
print(r, ':',employee[r])

After writing the above code (create dictionary python for loop), Ones you will print “ (r, ‘:’, employee[r]) “ then the output will appear as a “ 201: Cyrus 202: Kathilin 203: Mishal ”. Here, the for loop will iterate key-value in the dictionary.

You can refer to the below screenshot for Creating a dictionary python for loop.

Create dictionary python for loop

Create dictionary python with two lists

To create a dictionary python with two lists, we can use the zip() method. Here, one list element gets paired with the other list elements and the resultant will be a dictionary.

Example:

list1 = ['Red', 'Blue', 'Pink', 'Green']
list2 = ['4', '5', '8', '12', '10']
color_dictionary = dict(list1,list2)
print(color_dictionary)

After writing the above code (create dictionary python with two lists), Ones you will print “(color_dictionary)” then the output will appear as an ” Error: dict expected at most 1 argument and it got 2″.

You can refer to the below screenshot for Creating a dictionary python with two lists.

Create dictionary python with two lists

Example:

list1 = ['Red', 'Blue', 'Pink', 'Green']
list2 = ['4', '5', '8', '12', '10']
color_dictionary = dict(zip(list1,list2))
print(color_dictionary)

After writing the above code (create dictionary python with two lists), Ones you will print “(color_dictionary)” then the output will appear as a “{‘Red’: ‘4’, ‘Blue’: ‘5’, ‘Pink’: ’12’, ‘Green’: ’10’} ”. Here, the two list is converted to a dictionary by using zip it will pair together and it will create a dictionary of two list.

You can refer to the below screenshot for Creating a dictionary python with two lists.

Create dictionary python with two lists

Python create a dictionary from list of keys and values

The dictionary is created from a list of keys and respective values. By using python dictionary comprehension, we get a dictionary in python with key-value pairs.

Example:

my_key = ["Paras", "Jain", "Cyware"]
my_value = ["101", "102", "103"]
result = {k:v for k,v in zip(my_key,my_value)}
print(result)

After writing the above code (create a dictionary from a list of keys and values), Ones you will print “result” then the output will appear as a “ {‘Paras’: ‘101’, ‘Jain’: ‘102’, ‘Cyware’: ‘103’} ”. Here, the dictionary will be created from a list of key and values.

You can refer to the below screenshot for creating a dictionary from list of keys and values.

Python create dictionary from list of keys and values

Python sort dictionary by value

The built-in sorted() method is used to sort the dictionary by the value, we can use the two function together items() and sorted() for sorting the element according to the value and the lambda function will return the value.

Example:

my_value = {30: 'Tom', 18: 'Anisha', 20: 'Jim'}
store = dict(sorted(my_value.items(), key=lambda x: x[1]))
print("sorting by value: ", store)

After writing the above code (Python sort dictionary by value), Ones you will print ” store ” then the output will appear as a “ {18: ‘Anisha’, 20: ‘Jim’, 30: ‘Tom’} ”. Here, the dictionary will be sorted according to its value.

You can refer to the below screenshot for creating a python sort dictionary by value.

Python sort dictionary by value

Initialize empty dictionary python

In python, we can initialize an empty dictionary by not giving any elements inside the curly brackets in assignment statements. If the key and value are not present inside the curly brackets then it will be an empty dictionary in python.

Example:

name = {}
print(name)

After writing the above code (initialize empty dictionary python), Ones you will print ” name ” then the output will appear as a “ { } ”. Here, the empty dictionary is initialized and it will return an empty dictionary.

You can refer to the below screenshot for initialize empty dictionary python

Initialize empty dictionary python

We can also initialize empty dictionary in python by using built-in function ” dict() “ with no arguments.

Example:

name = dict()
print(name)

After writing the above code (initialize empty dictionary python), Ones you will print ” name ” then the output will appear as a “ { } ”. Here, the built-in dict() function is used with no arguments for creating the empty dictionary in python.

You can refer to the below screenshot for initialize empty dictionary python

Initialize empty dictionary python

Check if a value is in a dictionary in Python

In python, to check if a value is present in a dictionary or not we will use “dictionary.values()” to get the value if it is present then it will return true otherwise false.

Example:

my_dictionary = {"avinav": 111, "john": 222, "nick": 223}
check = 111 in my_dictionary.values()
print(check)

After writing the above code (check if a value is in a dictionary in Python), Ones you will print “check ” then the output will appear as a “ True ”. Here, dictionary.values() will check if the specified value is present in the dictionary or not.

You can refer to the below screenshot check if a value is in a dictionary in Python

Check if a value is in a dictionary in Python

You may like following Python tutorials:

In this Python tutorial, we learned about What is a Python dictionary and how to create a dictionary in python. And

  • Accessing elements from the dictionary
  • Create dictionary python with keys and values
  • Create dictionary python from list
  • Create dictionary python for loop
  • Create dictionary python with two lists
  • Python create a dictionary from list of keys and values
  • Python sort dictionary by value
  • Initialize empty dictionary python
  • Check if a value is in a dictionary in Python

Leave a Comment