In this Python tutorial, we will discuss everything about the Python Dictionary fromkeys() method. I will also show you, the syntax of fromkeys() dictionary method and how to use the dictionary fromkeys() method with a few examples.
Python dictionary fromkeys() method
The fromkeys() method is a built-in method in Python used for creating a new dictionary from a sequence of elements (like a list or tuple) with a specific value.
The syntax of the fromkeys() method is as follows:
In the above syntax:
'sequence'is the Python list of values that would be used as keys for the new dictionary.
'Value'is optional, which if provided will be set as the value for all elements in the Python dictionary. If not provided, the value defaults to
fromkeys() method in Python Dictionary Examples
Let’s dive into some examples to better understand the Python Dictionary fromkeys() method in action:
Example#1 Basic Usage
Suppose we are tracking the number of visitors from different US states to a website, and initially, we want to set the count to zero for all states.
states = ['California', 'Texas', 'New York', 'Florida', 'Illinois'] visitors = 0 # using fromkeys() method to create dictionary visitor_dict = dict.fromkeys(states, visitors) print(visitor_dict)
- In this code, we created a Python list
'states'containing the names of different states. We set the initial number of
- Then, we created a Python dictionary
'visitor_dict'using the fromkeys() method where each key (state name) is associated with the value 0.
- This could represent an initial state where no visitors have been logged from any state.
Example#2 Use Case without Value
The fromkeys() method can also be used without specifying a value. Let’s consider an example where we’re setting up a system to track whether we’ve received tax forms from various US states.
states = ['California', 'Texas', 'New York', 'Florida', 'Illinois'] # using fromkeys() method to create dictionary tax_forms_received = dict.fromkeys(states) print(tax_forms_received)
In this code, we created a list
'states' containing the names of different states. We then created a Python dictionary
'tax_forms_received' using the ‘fromkeys()’ method with these state names as keys.
Since we didn’t specify a value, each key (state name) is associated with the value None. This could represent an initial state where we have not received tax forms from any state, and we can update the value for each state to
'Received' once we receive the corresponding tax form.
Suppose we are working on a school project where we want to assign the same grade to a Python list of students initially. Here’s how we can do it:
students = ['Alice', 'Bob', 'Charlie', 'Dave'] grade = 'A' # Using fromkeys() method to create dictionary grade_dict = dict.fromkeys(students, grade) print(grade_dict)
In this code, we first defined a Python list of students. We then created a dictionary
grade_dict using the fromkeys() method, where each student (key) is initially assigned grade ‘A’ (value).
Let’s assume we’re tracking inventory for a Python list of products in a store. Initially, the stock for all items is set to None:
products = ['Apples', 'Bananas', 'Oranges', 'Pears'] stock = None # Using fromkeys() method to create dictionary stock_dict = dict.fromkeys(products, stock) print(stock_dict)
In this case, we’ve created a Python list of products, and we use the fromkeys() method to create a Python dictionary
stock_dict where each product (key) has its stock (value) initially set to None, indicating that we have not yet counted the stock for these items.
Python dictionary fromkeys() method is a handy function in Python when we need to initialize a dictionary with a common value for all keys. However, it’s important to note that since the method sets a mutable type like a list or a dictionary as a value, a change in one element might affect all others because they are all referencing the same object in memory.
In such cases, a Python dictionary comprehension or for loop would be a better option to create dictionaries.
You may also like to read the following articles:
- Python dictionary copy() method [With Examples]
- How to create a dictionary in Python
- Python dictionary len() method [With Example]
- Python dictionary setdefault() method [With Examples]
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.