AppDividend
Latest Code Tutorials

Python Dictionary FromKeys Example | fromKeys Method Tutorial

0

Python Dictionary FromKeys Example | fromKeys Method Tutorial is today’s topic. Python Dictionary fromKeys() method creates a new dictionary from a given sequence of elements with a value provided by a user. The fromKeys method returns the dictionary with specified keys and values. Sometimes there is a need to generate the dictionary from the given keys. The fromkeys() helps us achieve this very task with ease and using just a single method.

Python Dictionary FromKeys Example

The syntax of Dictionary fromKeys() is following.

dictionary.fromkeys(keys, value)

The keys parameter is required, and it is an iterable specifying the keys of the new dictionary.

The value parameter is optional and the value for all keys. The default value is None.

Python fromkeys() method returns a new dictionary with a given sequence of elements as the keys of the dictionary.

If a value argument is set, each element of the newly created dictionary is set to the provided value.

See the following example.

# app.py

StarWars = ('Luke', 'Vader', 'Ray', 'Yoda')
StarTrek = ('Spock')
universe = dict.fromkeys(StarWars, StarTrek)
print(universe)

The output is following.

Python Dictionary FromKeys Example

 

So, we have created a dictionary using fromKeys() method where we used StarWars as keys and StarTrek as value. In the final output, the values will be the same for all the keys because the values will be mapped to each key. If pass the no value to the dictionary then see the result. Check out the following code.

# app.py

StarWars = ('Luke', 'Vader', 'Ray', 'Yoda')
universe = dict.fromkeys(StarWars)
print(universe)

The output is following.

fromKeys Method Tutorial

 

It will give us the value as a None for all the keys.

Create a dictionary from Python List

We can create a dictionary from a Python List. See the following code.

# app.py

StarWars = ['Luke', 'Vader', 'Ray', 'Yoda']
StarTrek = 'Spock'
universe = dict.fromkeys(StarWars, StarTrek)
print(universe)

The output is following.

 

Create a dictionary from Python List

The fromKeys() can also be supplied with a mutable object as the default value. But in this case, a deep copy is made of a dictionary, i.e., if we append value in the original list, the append takes place in all the values of keys.

If a provided value is the mutable object (whose value can be modified) like the list, dictionary, etc., when a mutable object is modified, each element of a sequence also gets updated.

It is because each element is assigned a reference to a same object (points to the same object in the memory).

See the following code.

# app.py

StarWars = ['Luke', 'Vader', 'Rey', 'Yoda']
StarTrek = ['Spock']
universe = dict.fromkeys(StarWars, StarTrek)
print(universe)

StarTrek.append('James')
print(universe)

The output is following.

 

Python Dictionary FromKeys Example | fromKeys Method Tutorial

The solution to the above problem is Dictionary Comprehension. See the following code.

# app.py

StarWars = ['Luke', 'Vader', 'Rey', 'Yoda']
StarTrek = ['Spock']
universe = { key : list(StarTrek) for key in StarWars }
print(universe)

StarTrek.append('James')
print(universe)

The output is following.

 

fromKeys Method Tutorial Example

Finally, Python Dictionary FromKeys Example | fromKeys Method Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.