Skip to content

Instantly share code, notes, and snippets.

@SaraM92
Created September 24, 2021 05:38
Show Gist options
  • Save SaraM92/77f770fb3831ef2dcd3df07ab48bf99a to your computer and use it in GitHub Desktop.
Save SaraM92/77f770fb3831ef2dcd3df07ab48bf99a to your computer and use it in GitHub Desktop.
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