Q:

Write a program in Python to create a dictionary from the given sequence of keys and value provided by the user

0

Write a program in Python to create a dictionary from the given sequence of keys and value provided by the user

keys = {'Maths', 'English', 'Computer', 'Science'}
value = 'A'

All Answers

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

Solution

Python has predefined fromkeys() method to create a new dictionary from the given sequence of elements.

Syntax of Python fromkeys()
dictionary.fromkeys(sequence[, value])

Here, the sequence is taken as key in creating dictionary and the value parameter is taken as the value in the creating dictionary. The value is an optional parameter. It returns a new dictionary with the given sequence of elements as the keys of the dictionary.

This is the following solution to create a dictionary from the given sequence of keys and value provided by the user -

keys = {'Maths', 'English', 'Computer', 'Science'}
value = 'A'
items = dict.fromkeys(keys, value)
print items
Output of the above code

{'English': 'A', 'Maths': 'A', 'Computer': 'A', 'Science': 'A'}

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

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a list comprehension that returns all the pa... >>
<< Write a program in Python to read content from one...