Search code examples
pythonlistindexingsublist

Locate position of an item in a sublist through the main list?


I have a block of code that look like this:

customer_order1 = []
customer_order2 = []
customer_order3 = []
customer_order4 = []

all_orders = [customer_order1,customer_order2,customer_order3,customer_order4]

All the lists relating to the 'customer_order' only contain 1 item max in each of them. In the list customer_order1 a varible named 'Burger' is located. Ex:

customer_order1 = [Burger]

I'm trying to use the position of 'Burger' to find which list it belongs to in all_orders. Basically, 'Burger' is located in customer_order1, which is position [0] in the all_orders list. After that I can use the location of customer_order1 in all_orders later on in the project. Ex:

if x == all_orders[0]:
    print(" ")
    for y in range (int(number_of_orders1[0])):
      print("🍔", end=" ", flush=True)

To do this I tried using in index function:

x = all_orders.index(Burger)

And I thought that it would find which sublist 'Burger; is stored give me the location of that list inside all_orders. But instead I get:

ValueError: 'Burger(s)' is not in list

I know that 'Burger' (also known as 'Burger(s)') does exist in customer_order1 because previously I ran the command:

if any({Burger}.issubset(sublist) for sublist in all_orders):

This checked if 'Burger' was found in any of the lists inside all_orders.

I've said Burger way too many times. Anyway, could someone provide some help? I tried to provide as much information as possible. I'm new to Python and new to programming in general.


Solution

  • It's because you have a burger in a list, so you need to find "buger in a list" inside your all_orders Basicaly doing : x = all_orders.index([burger])

    Demo

    Here a verbose exemple

    >>> class Burger:
    ...     pass
    ...
    >>> burger = Burger()
    >>> burger
    <__main__.Burger object at 0x7fc6137830d0>
    >>> customer_order1=[burger,]
    >>> customer_order1
    [<__main__.Burger object at 0x7fc6137830d0>]
    >>> customer_order2 = []
    >>> customer_order3 = []
    >>> customer_order4 = []
    >>> all_orders = [customer_order1,customer_order2,customer_order3,customer_order4]
    >>> all_orders
    [[<__main__.Burger object at 0x7fc6137830d0>], [], [], []]
    >>> x = all_orders.index(burger)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: <__main__.Burger object at 0x7fc6137830d0> is not in list
    >>> x = all_orders.index([burger])
    >>> x
    0 # HERE you have your index