How to Create Dictionary From A Sequence in Python

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

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to Create Dictionary From A Sequence in Python." Ariessa Norramli | Sciencx - Friday February 26, 2021, https://www.scien.cx/2021/02/26/how-to-create-dictionary-from-a-sequence-in-python/
HARVARD
Ariessa Norramli | Sciencx Friday February 26, 2021 » How to Create Dictionary From A Sequence in Python., viewed ,<https://www.scien.cx/2021/02/26/how-to-create-dictionary-from-a-sequence-in-python/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Create Dictionary From A Sequence in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/26/how-to-create-dictionary-from-a-sequence-in-python/
CHICAGO
" » How to Create Dictionary From A Sequence in Python." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/02/26/how-to-create-dictionary-from-a-sequence-in-python/
IEEE
" » How to Create Dictionary From A Sequence in Python." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/26/how-to-create-dictionary-from-a-sequence-in-python/. [Accessed: ]
rf:citation
» How to Create Dictionary From A Sequence in Python | Ariessa Norramli | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.