I have a list and in that list, I am trying to find a specific string in that list by asking the user to enter in a word and I will print that word and the line that it is from
This is one list
new_list = ['An American', 'Barack Obama', '4.7', '18979'],
['An Indian', 'Mahatma Gandhi', '4.7', '18979'],
['A Canadian', 'Stephen Harper', '4.6', '19234']
For example, if I can input "ste" in the string and it should print the 3rd line as "Stephen Harper" is in there
I tried this but it did not work:
find_String = input("Enter a string you're looking for: ")
if find_String in new_list:
print(new_list)
else:
print("String not found!")
Well it doesn't work because of the following reasons :-
list
is only holding one list object, the first one, the rest 2 are not in the variable.if
statement, for it has to iterate through the 3 lists within the list
variable.The following code will work :-
L1 = [['An American', 'Barack Obama', '4.7', '18979'],
['An Indian', 'Mahatma Gandhi', '4.7', '18979'],
['A Canadian', 'Stephen Harper', '4.6', '19234']]
find_String = input("Enter a string you're looking for: ")
for data in L1:
for string in data:
if find_String.lower() in string.lower():
print(data)
exit(0)
else:
print("String not found!")