Created
March 19, 2022 20:46
-
-
Save thinmy/4909f9bc117ea1c1cf49f5724f6e6cdd to your computer and use it in GitHub Desktop.
#codesnippetsdev merge two lists into a dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
keys_list = ['A', 'B', 'C'] | |
values_list = ['blue', 'red', 'bold'] | |
#There are 3 ways to convert these two lists into a dictionary | |
#1- Using Python's zip, dict functionz | |
dict_method_1 = dict(zip(keys_list, values_list)) | |
#2- Using the zip function with dictionary comprehensions | |
dict_method_2 = {key:value for key, value in zip(keys_list, values_list)} | |
#3- Using the zip function with a loop | |
items_tuples = zip(keys_list, values_list) | |
dict_method_3 = {} | |
for key, value in items_tuples: | |
if key in dict_method_3: | |
pass # To avoid repeating keys. | |
else: | |
dict_method_3[key] = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment