Q:

Python program to create a dictionary from a sequence

belongs to collection: Python Dictionary Programs

0

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)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Dictionary Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to create a dictionary with integer... >>
<< Python program to create a dictionary using dict()...