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 »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.