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? By Shivang Yadav Last updated : September 18, 2023

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.

Problem statement

Here, we are creating a Python program in which we are creating a dictionary from a given sequence.

Creating a dictionary from a sequence

To create a dictionary from a given sequence,we can use the dict() function by passing the sequence from which we have to create a dictionary. The dict() function a built-in function in Python. It is used to create a dictionary.

Syntax

dictionary_name = dict(sequence)

Python program to create a dictionary from a sequence

# 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

The output of the above program is:

dict_a : {1: 'apple', 2: 'ball', 3: 'cat'}
1 : apple
2 : ball
3 : cat

Python Dictionary Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.