Skip to content
Advertisement

How to create a search function of a list with multiple organized elements?

I’m trying to create a function with two inputs (number, people) whereas it searches from a list with multiple organized elements for the correct number and outputs the correct information (which the output is named: user).

Here’s the list:

List = ([15209 Amanda Craig NC], [16428 Johnathan Smith OH], [12690 Samantha Owens IL], [17002 Mark Stenson NY], [13379 Francis Trent NC], [14904 Jake Peterson CA])

The number request (not a user-input):

14904
17002
19999

Desired output:

14904 Jake Peterson CA
17002 Mark Stenson NY
"User not found"

This is what I have at the moment. I’m not sure if I should use split, but then I have no clue what to do next.

people = ([15209 Amanda Craig NC], [16428 Johnathan Smith OH], [12690 Samantha Owens IL], [17002 Mark Stenson NY], [13379 Francis Trent NC], [14904 Jake Peterson CA])

user = people[i]

def search_user(number, people):
    ID = people.split()[0]
    if number != ID: #I'm not sure how I can find the numbers 
        return "User not found"
    else:
        return user

If anything is wrong, it is okay to scrap it. I’m a bit confused about where to even start.

Advertisement

Answer

And if you insist on using a Tuple of lists, you should fix the syntax as was advised, which could lead for example to:

users = ([15209, "Amanda Craig NC"], [16428, "Johnathan Smith OH"], [12690, "Samantha Owens IL"], [17002, "Mark Stenson NY"], [13379, "Francis Trent NC"], [14904, "Jake Peterson CA"])

def search_user(number, users):
    for user in users:
        if user[0] == number:
            return user
    return "User not found"
            
            
print(search_user(14904, users))
print(search_user(17002, users))
print(search_user(19999, users))

That said, a dictionary would be far more relevant to your needs, as per the previous answer.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement