How To Create Dictionary In Python?

Dictionary is a data type provided by Python programming language. Python dictionary is used to store key/value items. Every item consists of a key and value. The key can be an integer, string, or another type. Also, the value can be an integer, string, list, or an object. In this tutorial, we will examine how to create a dictionary in different ways.

Create Dictionary Providing Key/Value Pairs

The easies and organic way to create a dictionary is providing the items or key/value pairs by providing and assigning them into the newly create dictionary variable.

d1 = { "ahmet":1 , "ali":2 , "elif":3 }

d2 = { "ahmet":1 , 2:"ali" , "elif":3 }

d3 = { 1:"ahmet" , 2:"ali" , 3:"elif" }

Create Empty Dictionary

We can also create an empty dictionary with providing any item or key/value pair. We will do not provide any value like below. We can also add new items into dictionary after creation.

d = {}

d[1] = "one"

d[2] = "two"

d[3] = "three"

Create Dictionary Copying Another Dictionary

A new dictionary can be created by copying all items of another dictionary. This copy operation can be done in different ways but a dictionary type provides the copy() method which will copy all items or key/value pairs into the new dictionary. If we want we can change the items of the new dictionary later.

d1 = { "ahmet":1 , "ali":2 , "elif":3 }

d2 = d1.copy()

d2["ahmet"] = 10

Create Dictionary with fromkeys() Method

The fromkeys() method can be used to create a new dictionary where the keys can be specified as a list. The value of the created dictionary can be set to a default value or leaving without a default value which will be None.

d1 = dict.fromkeys( [ "ahmet" , "ali" , "elif" ] )
print(d1)


d2 = dict.fromkeys( [ "ahmet" , "ali" , "elif" ] , 10 )
print(d2)
Create Dictionary with fromkeys() Method

Create Dictionary From List

List and dictionary are named as iterable types where multiple iterable objects are stored. A dictionary can be created with a list where in order to set key and value properly tuple type will be used too. List contains multiple tuple where each tuple contains two items for key and value.

d1 = dict( [ ("ahmet",1) , ("ali",2) , ("elif",3) ] )

d2 = dict( [ ( 1 , "ahmet") , ( 2 , "ali") , ( 3 ,"elif") ] )

Create Dictionary From Tuples

Tuples are very similar to the dictionary and lists. New dictionary can be created by using tuple data type. Every dictionary item or key/value pair will be specified as a tuple where tuple contains two items. The dict() method is used to convert multiple tuples into a dictionary.

d1 = dict( ("ahmet",1) , ("ali",2) , ("elif",3) )

d2 = dict( ( 1 , "ahmet") , ( 2 , "ali") , ( 3 ,"elif") )

Create Dictionary with Integer Keys

Dictionary contains items where every item consist of from key and value pairs. Keys are generally string type but it is possible that using integers as keys. Below you can see some dictionary creation examples where keys are integer type.

d = { 1:"ahmet" , 2:"ali" , 3:"elif" }

Create Dictionary with Mixed Values

Dictionary can used different types as a value. Even keys should be a string or integer the values can be a string, an integer, a list, a tuple even another dictionary.

d = { 1:"ahmet" , "ali":2 , 3:(1,2,3) , 4:[1,2,3] , 5:{ 1:2 , "a":"b" } }

Create Nested Dictionary

Nested dictionary contains multiple dictionaries in different levels. In a nested dictionary items may be dictionary even items of the items can be a dictionary too.

d = { 1:{ 1:2 , "a":"b" } , 2:{ "c":"d" , "e":{ 10:20 } } }

Leave a Comment