If i have a dictionary and a list:
dict = {"A":1, "B",:2, "C":3}
list = ['C', 'A', 'B']
How to get the output list of both combined:
output_list = ['3', '1', '2']
If i have a dictionary and a list:
dict = {"A":1, "B",:2, "C":3}
list = ['C', 'A', 'B']
How to get the output list of both combined:
output_list = ['3', '1', '2']
You could use a list comprehension:
d = {'A': 1, 'B': 2, 'C': 3}
l = ['C', 'A', 'B']
output_list = [d[e] for e in l]
print(output_list)
Explanation: for each element e in the list l, we lookup the corresponding value of the key e in the dictionary d.
This gives the output:
[3, 1, 2]
And if you wanted the output to be a list of strings, you would convert the numbers with str:
output_list = [str(d[e]) for e in l]
This gives the output:
['3', '1', '2']
(And the usual advice is not to use dict/list as variable names -- they are builtins already).
#rename
input_dict = {"A":1, "B",:2, "C":3}
input_list = ['C', 'A', 'B']
out = [str(input_dict[_e]) for _e in input_list]
you can use this type of code :
dict_ = {"A":1, "B":2, "C":3}
list_ = ['C', 'A', 'B']
for i in range(len(list_)) :
output_list.append(dict_[list_[i]])