×

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

Compound Interest GUI Calculator using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Python Dictionary popitem() method

Python Dictionary popitem() method The dictionary.popitem() method in Python removes the item that was last inserted into the dictionary. Syntax dictionary.popitem() Parameter NA Return This method returns an arbitrary element (key, value) pair from the given dictionary...

1 minute read.

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

Create First GUI Application 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...

6 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Python pycountry

What is Pycountry? Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations...

4 minutes read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

Python exit commands

exit(), quit(), sys.exit(), os._exit() In this tutorial, we will study exit commands used in the Python programming language. Python is undoubtedly the choice of programmer and this is because of the in-built...

3 minutes read.

Import Module in Python

In this article, you will learn everything about “ Import ” in Python. Modules in python that are already created can be accessed and used in another code by importing the...

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

Cx_Oracle Python with 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...

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

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

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