×

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

wxPython Panel class

wxPython Panel class The Widgets which is shown in the frame of GUI window such as text box, buttons, static text etc. are put inside the panel class of the wxpython...

2 minutes read.

How To Take Multiple Inputs In Python

In C language, we make use of the scanf() function to obtain the values from the user and store it in the variable. Coming to the Python language, we use the...

5 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.

Best Database in Python

In this tutorial, Firstly, we will understand what the term database means. Further, we will see the various databases supported in Python and when to use which one. Further, we...

7 minutes read.

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python List reverse() method

 Python List reverse() method The list.reverse () method in Python reverses the elements of the list in place. Syntax list.reverse() Parameter NA Example 1 # Python program explaining # the list.reverse() method weekList = ['Sun','Thurs','Mon','Fri', 'Tues', 'Wed'] print("Actual week list: ",weekList) #...

1 minute read.

Python Subprocess Call Example

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

4 minutes read.

Character Set in Python

A collection of legal characters that will recognize by a programming language known as a Character set. Taking python programming language as an instance. The set of characters supported by the...

6 minutes read.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

How to make a firewall in Python?

Firewall: The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security...

3 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Python hasattr() function

Python hasattr() function The hasattr() function in Python returns a Boolean value ‘True’ if the given object has the specified attribute, else it returns False. Syntax hasattr(object, name) Parameter object: it is a required parameter which represents an object. attribute:...

1 minute read.

Difference between Yield and Return in Python

Python yield statement The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns...

3 minutes read.