user3708579 user3708579 -4 years ago 156
Python Question

How to make one element in a list call an element in another list using python?

so say i have list1 and list2.

list1 = [n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,star,pound]

list2 = [1,2,3,4,5,6,7,8,9,0,star,pound]


How to i write a function that when an element of list one is called, the corresponding element in list2 is called?

this may have been answered in another post but I have not been able to find and / or understand how to do this.

---Edit---

John Coleman's comment answered my question, but to help others reading this in the future I will clarify a few things.

"call" might have been the wrong term to use, I'm definitely a novice at programming. Here is how i used the code with the help from this thread.

n1 = 5
n2 = 6
n3 = 13
n4 = 19
n5 = 26
n6 = 12
n7 = 16
n8 = 20
n9 = 12
n0 = 4
star = 17
pound = 27

inputs = [n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,star,pound]
numbers = [1,2,3,4,5,6,7,8,9,0,star,pound]
shows = dict(zip(inputs,numbers))
count = 1

def loop2():
for input in inputs:
if GPIO.input(input) == False:
print '%s' % shows['input']

Answer Source

You can use .index() to look up where an element of one of the two lists occurs, and then feed that index to the other list:

>>> list1 = ['n1','n2','n3','n4','n5','n6','n7','n8','n9','n0','star','pound']
>>> list2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'star', 'pound']
>>> list2[list1.index('n5')]
'5'

Alternatively, you can create a dictionary linking the two lists:

>>> d = dict(zip(list1,list2))
>>> d['n5']
'5'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download