Python Dictionary

How to create dictionary in Python?

(curly braces) {} creates a key-value pair with a colon (:) in the bracket and a single dictionary is created separately with a comma (,) for each pair. In addition, you can also create a dictionary with the dict () function. Let's look at some examples:

Try this code:

# empty dictionary

my_dict = {}

# dictionary with integer keys

my_dict = {1: 'apple', 2: 'ball'}

# dictionary with mixed keys

my_dict = {'name': 'Maruf', 1: [2, 4, 3]}

# using dict()

my_dict = dict({1:'apple', 2:'ball'})

# from sequence having each item as a pair

my_dict = dict([(1,'apple'), (2,'ball')])
H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center