tdammon tdammon - 1 year ago 68
Python Question

How do I write the index for a list of lists

I have a list of 3 lists each containing a random integer between 1 and 9:

lists= [[1,3,5],[2,4,6],[7,8,9]]

I ask the user to select any single digit number. I am making a program that finds the number one less than the user inputs and then decides if the next number in the list (assuming it is not the end of the list) is bigger or smaller.

for x in lists:
for i in x:
if i= user_choice-1:

Here I am stuck.

Lets say the user_choice is 3. I want the program to find the number 3-1=2 in the nested lists and then compare the number following 2 (in this case 4) to the user_choice.

Answer Source

If I understand correctly, you want:

for x in lists:
    for i in range(len(x)):
        if x[i] == user_choice-1 and i < len(x)-1:
            if x[i+1] > x[i]:
                #Next value is bigger...
                #Next value is smaller...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download