Home »
Python »
Python programs
Python program to create a dictionary from a sequence
Here, we are going to learn how to create a dictionary from a sequence in Python?
Submitted by Shivang Yadav, on March 23, 2021
In Python programming language, a dictionary is a collection of an unordered collection of data values in the form of key-value pair. And, a sequence is a generic term for an ordered set.
Here, we are creating a Python program in which we are creating a dictionary from a given sequence.
To create a dictionary from a given sequence – we can use the dict() function, which is a built-in function in Python. It is used to create a dictionary.
Syntax:
dictionary_name = dict(sequence)
Program:
# Python program to
# create a dictionary from a sequence
# creating dictionary
dic_a = dict([(1,'apple'), (2,'ball'), (3,'cat')])
# printing the dictionary
print("dict_a :", dic_a)
# printing key-value pairs
for x,y in dic_a.items():
print(x,':',y)
Output:
dict_a : {1: 'apple', 2: 'ball', 3: 'cat'}
1 : apple
2 : ball
3 : cat
Python Dictionary Programs »
ADVERTISEMENT
ADVERTISEMENT