DekGenius.com
PYTHON
create dictionary python from two lists
>>> keys = ['a', 'b', 'c']
>>> values = [1, 2, 3]
>>> dictionary = dict(zip(keys, values))
>>> print(dictionary)
{'a': 1, 'b': 2, 'c': 3}
convert 2 lists to a dictionary in python
""" How to convert 2 lists into a dictionary in python """
# list used for keys in dictionary
students = ["Angela", "James", "Lily"]
# list of values
scores = [56, 76, 98]
# convert lists to dictionary
student_scores = dict(zip(students, scores))
# print the dictionary
print(student_scores)
""" result should be
student_scores: -{
Angela: 56,
James: 76,
Lily: 98
},
"""
Convert two lists into a dictionary in Python
>>> students = ["Cody", "Ashley", "Kerry"]
>>> grades = [93.5, 95.4, 82.8]
>>> {s: g for s, g in zip(students, grades)}
{'Cody': 93.5, 'Ashley': 95.4, 'Kerry': 82.8}
create dict from two lists
keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
make dict from 2 lists
keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': 1, 'b': 2, 'c': 3}
create a dict from two lists
zip_iterator = zip(keys_list, values_list)
dictionary from two list
feat_dic = dict(zip(X.columns, feat_imp))
convert 2 lists into dictionary
# convert 2 list into dictionary
a = ['gsw','lakers','clippers']
b = [1,2,3]
my_dict = dict(zip(a,b))
print(my_dict) # {'gsw': 1, 'lakers': 2, 'clippers': 3}
python two list into dictinaray
index = [1, 2, 3]
languages = ['python', 'c', 'c++']
dictionary = dict(zip(index, languages))
print(dictionary)
© 2022 Copyright:
DekGenius.com