1

I been searching but nothing quite answers what I'm looking for. For this assignment we were given a list, but I need to print out specific elements from that list so it could look something like this, but I need to use user input to search that list :/

Employee Name: Jeffery Medina
Salary: 101442.00
Age: 23

This is my list

lst=[('Jeffery Medina','Officer','1254','101442.00','23'),('Katrina Peters','Officer','3423','94122.00','45'),('Kim Alan','Captain','6434','101592.00','29'),('Vincente Mendez','Officer','3235','110064.00','32'),('Chris Boalen','Captain','8769','50436.00','56'),('James Vito','Clerk','4451','23500.00','61'),('Terry George','Fireman','3342','93354.00','32'),('Zaid Dane','Officer','2345','84054.00','19'),('Ernesto Rodriguez','Officer','9091','87006.00','35'),('Josefine White','Fireman','3401','102228.00','26'),('Mario Infante','Officer','3234','84054.00','22'),('Juan Almonte','Fireman','4103','91272.00','50'),('Kevin Smith','Fireman','3450','111492.00','62'),('Abdum Smith','Captain','2234','95484.00','20'),('Juan Gomez','Clerk','9023','23890.00','49')]

And for user input i just used this: name=input("Who are you looking for? :")

Thanks

5 Answers5

1

With list comprehension:

data = [('Jeffery Medina','Officer','1254','101442.00','23'),('Katrina Peters','Officer','3423','94122.00','45'),('Kim Alan','Captain','6434','101592.00','29'),('Vincente Mendez','Officer','3235','110064.00','32'),('Chris Boalen','Captain','8769','50436.00','56'),('James Vito','Clerk','4451','23500.00','61'),('Terry George','Fireman','3342','93354.00','32'),('Zaid Dane','Officer','2345','84054.00','19'),('Ernesto Rodriguez','Officer','9091','87006.00','35'),('Josefine White','Fireman','3401','102228.00','26'),('Mario Infante','Officer','3234','84054.00','22'),('Juan Almonte','Fireman','4103','91272.00','50'),('Kevin Smith','Fireman','3450','111492.00','62'),('Abdum Smith','Captain','2234','95484.00','20'),('Juan Gomez','Clerk','9023','23890.00','49')]
name = input('Who are you looking for: ')
print([x for x in data if name in x[0]])

Output:

Who are you looking for: Jeffery
[('Jeffery Medina', 'Officer', '1254', '101442.00', '23')]
Alderven
  • 7,569
  • 5
  • 26
  • 38
0
  1. Do not use the keyword list as a variable name.

  2. You could iterate over the length of the list and check if the name is equal to any of its first index's first element.

Hence:

lst=[('Jeffery Medina','Officer','1254','101442.00','23'),('Katrina Peters','Officer','3423','94122.00','45'),('Kim Alan','Captain','6434','101592.00','29'),('Vincente Mendez','Officer','3235','110064.00','32'),('Chris Boalen','Captain','8769','50436.00','56'),('James Vito','Clerk','4451','23500.00','61'),('Terry George','Fireman','3342','93354.00','32'),('Zaid Dane','Officer','2345','84054.00','19'),('Ernesto Rodriguez','Officer','9091','87006.00','35'),('Josefine White','Fireman','3401','102228.00','26'),('Mario Infante','Officer','3234','84054.00','22'),('Juan Almonte','Fireman','4103','91272.00','50'),('Kevin Smith','Fireman','3450','111492.00','62'),('Abdum Smith','Captain','2234','95484.00','20'),('Juan Gomez','Clerk','9023','23890.00','49')]

name = input("Who are you looking for? :")

for i in range(len(lst)):
    try:
        if name == lst[i][i]:
            print("Employe Name: {} \nSalary: {} \nAge: {} ".format(lst[i][i], lst[i][i+3], lst[i][i+4]))
    except IndexError:
        pass

OUTPUT:

Who are you looking for? :Jeffery Medina
Employe Name: Jeffery Medina 
Salary: 101442.00 
Age: 23 

EDIT:

Another solution, using regex that would eliminate the need of case sensitivity.

for i in range(len(lst)):
    try:
        if re.search(name, lst[i][i], re.IGNORECASE):
            print("Employe Name: {} \nSalary: {} \nAge: {} ".format(lst[i][i], lst[i][i+3], lst[i][i+4]))
    except IndexError:
        pass

OUTPUT:

Who are you looking for? :jeFFerY mEdinA
Employe Name: Jeffery Medina 
Salary: 101442.00 
Age: 23 
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0
list=[('Jeffery Medina','Officer','1254','101442.00','23'),('Katrina Peters','Officer','3423','94122.00','45'),('Kim Alan','Captain','6434','101592.00','29'),('Vincente Mendez','Officer','3235','110064.00','32'),('Chris Boalen','Captain','8769','50436.00','56'),('James Vito','Clerk','4451','23500.00','61'),('Terry George','Fireman','3342','93354.00','32'),('Zaid Dane','Officer','2345','84054.00','19'),('Ernesto Rodriguez','Officer','9091','87006.00','35'),('Josefine White','Fireman','3401','102228.00','26'),('Mario Infante','Officer','3234','84054.00','22'),('Juan Almonte','Fireman','4103','91272.00','50'),('Kevin Smith','Fireman','3450','111492.00','62'),('Abdum Smith','Captain','2234','95484.00','20'),('Juan Gomez','Clerk','9023','23890.00','49')]

name = input("Who are you looking for? :")
for i in range(len(list)):
    if list[i][0] == name:
        print("Employe Name: {} \nSalary: {} \nAge: {} ".format(name,str(list[i][3]),str(list[i][4])))

Result:

Who are you looking for? : Jeffery Medina

Employee Name: Jeffery Medina

Salary: 101442.00

Age: 23

ncica
  • 7,015
  • 1
  • 15
  • 37
0

You have a list of tuples. Your user input would be a name, so a basic approach would be to check every tuple for this specific name:

name = input("Who are you looking for? :")
lst = [('Jeffery Medina','Officer','1254','101442.00','23'),('Katrina Peters','Officer','3423','94122.00','45'),('Kim Alan','Captain','6434','101592.00','29'),('Vincente Mendez','Officer','3235','110064.00','32'),('Chris Boalen','Captain','8769','50436.00','56'),('James Vito','Clerk','4451','23500.00','61'),('Terry George','Fireman','3342','93354.00','32'),('Zaid Dane','Officer','2345','84054.00','19'),('Ernesto Rodriguez','Officer','9091','87006.00','35'),('Josefine White','Fireman','3401','102228.00','26'),('Mario Infante','Officer','3234','84054.00','22'),('Juan Almonte','Fireman','4103','91272.00','50'),('Kevin Smith','Fireman','3450','111492.00','62'),('Abdum Smith','Captain','2234','95484.00','20'),('Juan Gomez','Clerk','9023','23890.00','49')]

for x in lst:
    if name in x:
        do_something_with_this_tuple(x)
trotta
  • 1,232
  • 1
  • 16
  • 23
0

You have to check each person in the list, and if the name of the user input matches the first item of each person (name), print out all their info.

Also, I would suggest renaming your list to something besides “list” since it can get confusing later on!

while True:
    name = input("Who are you looking for?: ")

    for person in people:
        if person[0] == name:
            print("Name: {},\nRank: {},\nNumber: {},\nSalary: {},\nAge: {}".format(person[0],person[1],person[2],person[3],person[4]))
            break
        else:
            print("This person does not exist. Try Another")

Hope this helps, -Nate