-1

How can I make a list equal if it has the same strings but in a different order? For an example.. The customer wants a Pineapple and Ham pizza.

Heres the function for when I makes a pizza for the customer. I add Pineapples than I add Ham to mPlayer.last_pizza list.

def add_toppings():
    choice_input = input(f"""
    Toppings:{mPlayer.last_pizza} Baked:{mPlayer.baked}  Sliced:{mPlayer.cut}
                C - Add Cheese
                H - Add Ham
                J - Add Jalapenos
                P - Add Pineapples
                A - Add Pepperoni's
                S - Add Sausages
                Clear - Clear Toppings
                Exit - Exit

            **** Make a Selection ****

After I exit the add_toppings function to go to the delivering method. I type "1" and the if statement gets executed.

def delivering(self, e, b, c, d, ):
    print(mPlayer.last_pizza)
    choice_input = input(f"""

                    1 - {self.address}
                    2 - {b}
                    3 - {c}
                    4 - {d}
                    5 - {e}

        **** Choose the customer house to deliver the pizza ****

    """)
    if choice_input == '1' and mPlayer.last_pizza == self.customerdir:
        print(self.name + ": Thank you so much you are a life saviour!")
        mPlayer.last_pizza.clear()

But if the toppings are in a different order like "Ham", "Pineapples" the if statement never gets executed.

How can I make it work even if the toppings aren't in the same order as the customer's list?

Thank you so much.

Jac Mulder
  • 47
  • 5
  • Does this answer your question? [How do Python's any and all functions work?](https://stackoverflow.com/questions/19389490/how-do-pythons-any-and-all-functions-work) You can use `all()` to check if all of the elements in one list are in the other, or you can `sort()` both and compare – G. Anderson Nov 30 '20 at 17:16
  • 5
    Just call ```sort()``` on both lists to make sure they are in the same order. – sarartur Nov 30 '20 at 17:17
  • 4
    Or use sets instead of lists. – ddastrodd Nov 30 '20 at 17:17
  • 1
    @ddcastrodd that depends if the count of items matters. With sets `[1, 1, 2]` will be equal to `[2, 1]` which might not always be desirable – Tomerikoo Nov 30 '20 at 17:26
  • @G.Anderson see above regarding the use of `all` – Tomerikoo Nov 30 '20 at 17:30

1 Answers1

1

It's not clear what you are doing here, since I have no idea what the mPlayer class is doing. However, from what you describe I think you can sort the lists:

>>> x = [2,1,3]
>>> y = [3,2,1]
>>> x==y
False
>>> x.sort()
>>> y.sort()
>>> x==y
True
user128316
  • 28
  • 4