Home > Net >  How to search a list an print or return items that match?
How to search a list an print or return items that match?

Time:10-03

I'm having problems with this, basically I'm trying to search from user input an license ID, like 235467-9 and returning or printing all the items on the list that match the search.

def menu_2():
    print("===================================")
    print("             Tickets               ")
    print("===================================")
    vid=str(input("Enter the ID: "))

user is a list at the beginning of the program that contains sold train tickets that have the person ID, type and price, each item on the list is separated like this :

 [5134123-5, first class, $20], [5462112-3, business class, $10].

When I'm able to search the list and print if the ID was found or not, idk how to print or return the items that match the search.

To anyone able to help me with this, thanks a lot.

CodePudding user response:

Here's how you solve this

Here's the list of users

l = ["5134123-5", "first class", "$20"], ["5462112-3", "business class", "$10"]

We loop through each elements in the loop and check if the id is in the element.

id = input() //User input 
for i in l:
    if id in i:
        print(*i)

This would print the elements whose Id matched the user Id. You could break out of the loop if the Id's are unique.

  • Related