Tianxiang Wang Tianxiang Wang - 1 year ago 80
Python Question

how to make a relation between two lists of number in python

The specific question is like this:

I have

two lists
, namely:

lst1 = [1,2,5,6,7];
lst2 = [11,12,13,14,15];

I would like to build a relation of those lists to match with the corresponding number of the other list, like
and if I input
, then it will automatically generate
. I'm new to python so I don't know if it is doable or could someone give me some advise or ideas, thanks in advance!

Answer Source

you could create a dictionary with the elements of the first list as keys and the elements of the second list as values:

lst1 = [1,2,5,6,7]
lst2 = [11,12,13,14,15]

dct = dict((a, b) for a, b in zip(lst1, lst2))
# or even:
# dct = {a: b for a, b in zip(lst1, lst2)}
print(dct)  # {1: 11, 2: 12, 5: 13, 6: 14, 7: 15}
print(dct[1], dct[5], dct[7])  # 11 13 15
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download