Python dictionary fromkeys()

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'

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
Python Fromkeys



Related Articles

Python program for Prime Number
Odd Even Program in Python
Python program to convert Celsius to Fahrenheit
Python Convert XML to CSV
Python Text to Speech
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml




Read more articles


General Knowledge



Learn Popular Language