This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to create a dictionary from a sequence in Python.
Let’s say you have a sequence of cardinal directions.
keys = {'North', 'South', 'East', 'West'}
Create Dictionary From A Sequence in Python
In order to create a dictionary with keys and values, you can use the dict.fromkeys()
method.
keys = {'North', 'South', 'East', 'West'}
# Value for the sequence of keys
value = 'Cardinal Direction'
# Display dictionary
print(dict.fromkeys(keys, value))
# {'North': 'Cardinal Direction', 'West': 'Cardinal Direction', 'East': 'Cardinal Direction', 'South': 'Cardinal Direction'}
Note: The dict.fromkeys()
method functions by creating a dictionary from a sequence of keys with value
The post How to Create Dictionary From A Sequence in Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-26T12:34:46+00:00) How to Create Dictionary From A Sequence in Python. Retrieved from https://www.scien.cx/2021/02/26/how-to-create-dictionary-from-a-sequence-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.