4

I'm trying to do this program that tells me in which list I can find a "part" or value that I'm looking for. I need to get the name of the list in which the part is found. Each list is a location for the parts, and I made a list of lists to traverse through it all in a loop.

list1=[1,5,8,3,9]
list2=[18,56,82,31,91]
list3=[10,51,2,4,70]
lists=[list1, list2, list3]

def lookPart():
    part = int(input("What part are you looking for? "))
    for l in lists:
        for i in l:
            if i==part:
                print("Your part is here: ", l)

while 1:
    lookPart()
    print("")

Assuming Im looking for part 5:

I expect it to print:

Your part is here: list1

But instead I get:

Your part is here: [1,5,8,3,9]

EDIT: Thank you to all those of you who suggested using a dictionary. I will do that, but is there a way to create the dictionary using a loop? I will be dealing with a couple of hundred lists so making a dictionary manually would be very time-consuming.

  • 1
    Use a dictionary instead of a list of lists. `dicts = {"list1": list1, "list2": list2, "list3": list3}` and change the inner `for` loop to: `for list_name, l in dicts.items()`. Then print out `list_name` when you find your part. – pault May 23 '19 at 17:04
  • Thanks for the advice. As a beginner, I have another question. I plan on dealing with a couple hundred "lists". How can I create the dictionary with a loop to avoid having to define everything manually? – Marco Lopez May 23 '19 at 17:13
  • Seems like you want to [create a variable number of variables](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables). Hard to answer specifically without seeing a [mcve]. – pault May 23 '19 at 17:15
  • 1
    You say you have *a couple of hundred lists,* how are they created? What do their names look like? Are they all `list1, list2, list3, list4, list5, ...`? – Amir Shabani May 23 '19 at 17:25
  • So basically it will be taken from an excel document. One row contains all the lists, while the columns contain the values for each list (row). – Marco Lopez May 23 '19 at 19:14

2 Answers2

2

You could use a dictionary:

list_dict = {'list_1': [1,5,8,3,9], 'list_2': [18,56,82,31,91], 'list_3': [10,51,2,4,70]}
search_value = int(input('What value are you looking for? '))
for key, value in list_dict.items():
  if search_value in value:
    print(f'Your search value of {search_value} was found in {key} i.e. {value}')

Example Usage:

What value are you looking for? 5
Your search value of 5 was found in list_1 i.e. [1, 5, 8, 3, 9]

Note: Using in to check if an element exists in a list internally uses a loop so you don't have to explicitly check each element like you are currently doing.

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
1

As suggested by @pault; below is the script. Put all list in a dictionary using the list name as keys. Then print the key instead of the value.

list1=[1,5,8,3,9]
list2=[18,56,82,31,91]
list3=[10,51,2,4,70]
lists={'list1':list1, 'list2':list2, 'list3':list3}

def lookPart():
    part = int(input("What part are you looking for? "))
    for k, l in lists.items():
        for i in l:
            if i==part:
                print("Your part is here: ", k)

while 1:
    lookPart()
    print("")
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38