DekGenius.com
PYTHON
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
},
"""
how to convert multi list to dict
# Your Data list
names = ["john", "paul", "george", "ringo"]
job = ["guitar", "bass", "guitar", "drums"]
status = ["dead", "alive", "dead", "alive"]
dict_data = [{'name': name, 'job': job, 'status': status} for name,job,status in zip(names,jobs,statuses)]
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)
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}
how to convert multi list into dict in python
from collections import OrderedDict
o = collections.OrderedDict()
for i in data:
o.setdefault(i[0], []).append(i[1])
2 liste to a dictionary
d = {}
for i in list1:
for j in list2:
d[i] = j
print d
2 liste to a dictionary
keys = tel.keys()
values = tel.values()
python two list into dictinaray
index = [1, 2, 3]
languages = ['python', 'c', 'c++']
dictionary = dict(zip(index, languages))
print(dictionary)
© 2022 Copyright:
DekGenius.com