×

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

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

Python Sys Stdout

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

How to Install Matplotlib in Python?

How to Install Matplotlib in Python The speed at which the enormous amount of data is generating has become a huge aid in understanding what's going on currently in the market....

4 minutes read.

Python min() function

Python min() function returns you the minimum value element in an iterable. We can also use this function to determine the smallest value among various arguments passed to this function. We...

4 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.

Defaultdict in Python

In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries...

3 minutes read.

Python input() function

Python input() function The input() function in Python reads a line from input and converts it to a string (stripping a trailing newline). Syntax input([prompt]) Parameter prompt: This function represents a string, depicting a default...

1 minute read.

Python Data Structures

Python Data Structures: Python is a programming language used worldwide for various fields such as building dynamic websites, artificial intelligence and many more. However, there is data that plays a...

18 minutes read.

First Unique Character in a String Python

This article aims to introduce you to strings and how to create a string in Python, and then we will solve a simple yet exciting DSA (Data Structures and Algorithms)...

3 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

How to print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.

Python Errors and exceptions

Exceptions and errors are the obstacles a programmer constantly faces while writing a program. Firstly, we need to understand what are errors and exceptions and the difference between these two...

6 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.