×

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method

The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values.

Syntax

dictionary.fromkeys(sequence[, value])

Parameter

sequence- This parameter represents the sequence of elements which is to be used as keys for the new dictionary.

value(optional)- This argument signifies the value which is set for each element of the dictionary.

Return

This method returns a dictionary with the specified keys and values.

Example 1

 # Python program explaining
 # the dictionary.fromkeys() method
 dictionary={"banana": "apple",
   "orange": "mango",
   "grapes": 5
 }
 # initializing the sequence
 sequence = ('Monday', 'Tuesday', 'Wednesday')
 # initializing the key
 key = 1
 # creating a new dictionary from the given sequence of elements
 dict = dictionary.fromkeys(sequence, key)
 # returning a dictionary with the specified keys and values.
 print(dict) 

Output

{'Tuesday': 1, 'Monday': 1, 'Wednesday': 1}

Example 2

 # Python program explaining
 # the dictionary.fromkeys() method
 # initializing sequence  
 vowels = { 'a', 'e', 'i', 'o', 'u' } 
 # using fromkeys() to convert sequence to dict  
 # initializing with None 
 dictionary = dict.fromkeys(vowels) 
 # Printing created dict 
 print ("The newly created dict with no values = " + str(dictionary)) 
 # using fromkeys() to convert vowels to dict  
 # initializing with 9 
 dictionary2 = dict.fromkeys(vowels, 9) 
 # Printing the dict 
 print ("New created dict with 9 as its value = " + str(dictionary2)) 

Output

The newly created dict with no values = {'e': None, 'i': None, 'u': None, 'o': None, 'a': None}
New created dict with 9 as its value = {'e': 9, 'i': 9, 'u': 9, 'o': 9, 'a': 9}


Related Topics

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Python delattr() function

Python delattr() function The delattr() function in Python is used to delete the named attribute from the object, with the prior permission of the object. Syntax delattr(object, name) Parameter object : This parameter represents the object from which...

1 minute read.

Python all() Function

Python all() Function The all() function in Python returns a Boolean value ‘True’ if all the items in iterable are true or if the iterable object is empty, else it returns False. Syntax all(iterable) Parameter Iterable: An iterable object...

1 minute read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Spark and Python for big data with pyspark github

Pyspark: Apache Spark is a computational engine that handles large data sets using parallel and batch processing. Apache Spark is an open-source, distributed computing framework and collection of libraries for real-time,...

3 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.