0

I have a two-dimensional list that has about 1000 elements. It looks something like this:

myList = [['1', 'John', 'Doe', 'jdoe@email.com', '234-35-2355'], ['2', 'Rebecca', 'Swan', 'rswan@email.com', '244-56-4566'], ['3', 'Terry', 'Smith' 'tsmith@email.com', '345-45-1234']]

Index [1] is their first name, and I want to search this list (that has 1000 people, only used 3 for simplicity), to see if they are in the list, and if they are, print their information, without using Numpy.

So far I have:


firstName = input('Enter the first name of the record: ')

row = len(myList)

found = False
for row in my list:
    for element in row:
        if element == firstName:
            found = True
            break

    if found:
        break

if found:
    print('User ID: ', myList[0])
    print('First Name: ', myList[1])
    print('Last Name: ', myList[2])
    print('Email: ', myList[3])
    print('Login IP: ', myList[4])

else:
    print('There is no record found with first name', firstName)

Now this seems to be working to find if the person is there or not, however I am having trouble with printing the information after, because I do not know how to find the index of the person, I believe if I had the index the print would be something like myList[index][1]

EDIT: Okay I see that it was a simple fix of changing myList[1] to row[1]. Now say you search a name and two people in the list have the same name and you want to print both sets of information, how would I go about that?

AMC
  • 2,642
  • 7
  • 13
  • 35
  • 1
    you don't need the index. You have the object you want assigned to `row` – juanpa.arrivillaga Apr 07 '20 at 18:27
  • After you find the name and break out of the for loop `row` should still point to the info you need. If you also want the index use [enumerate](https://docs.python.org/3/library/functions.html#enumerate) with the *outer* for loop. – wwii Apr 07 '20 at 18:28
  • Okay I see, thank you! Now say you search a name and two people in the list have the same name and you want to print both sets of information... how would I go about that? –  Apr 07 '20 at 18:32
  • Keep all the indices for use later or just print them when you find them. – wwii Apr 07 '20 at 18:33
  • 1
    Does this answer your question? [Python search in lists of lists](https://stackoverflow.com/questions/1156087/python-search-in-lists-of-lists) .. [this answer](https://stackoverflow.com/a/1158136/2823755) to that question should do it. – wwii Apr 07 '20 at 18:37
  • As an aside, variable and function names should generally follow the `lower_case_with_underscores` style. – AMC Apr 07 '20 at 18:39

1 Answers1

0

One of many possible ways:

def findByName(name, lst):
    return filter(lambda x: x[1] == name, lst)

for item in findByName("John", myList):
    print(item)

This yields

['1', 'John', 'Doe', 'jdoe@email.com', '234-35-2355']


Or directly with a listcomp:
persons = [entry for entry in myList if entry[1] == name]
Jan
  • 42,290
  • 8
  • 54
  • 79