user1752061
user1752061

Reputation: 11

Search for the index of an element in A list, and if not found, let the user know

I have a basic program that takes an existing list, asks for user input, and searches for the elements of the user's input in the list. I want it to find what index it is at, and print the index. When I enter a value that isn't in the list, I get the error message: "ValueError: 7 is not in list"

How do I stop the program from giving the error message, or to let the user know the value was not in the list?

Edit, adding code:

L = [4, 10, 4, 2, 9, 5, 4]
L2 = []
print ("L = ",L)
x = input("Enter an element to search for in the list: ")
x = int(x)

m = L.index(x)
L2.append(m)
print("Found occurences at indexes: ",L2)

I'm just trying to find at what index all the occurrences are at, and if there aren't any, print an error message.

Upvotes: 1

Views: 1564

Answers (5)

eumiro
eumiro

Reputation: 212895

.index is not the way to go, since it finds only the first occurrence of the element. Iterate over the whole list and match individual elements:

L2 = [i for i,a in enumerate(L) if a == x]
if L2:
    print "Element found at positions: ", L2
else:
    print "Element is not in the list"

L2 is an empty list or contains all indexes where x can be found within L.

EDIT: The "hard" way without enumerate:

L2 = []
i = -1
while x in L[i+1:]:
    i = L[i+1:].index(x) + i + 1
    L2.append(i)

Returns the same results, i.e. empty list if nothing is found.

Upvotes: 4

Myjab
Myjab

Reputation: 944

For this you an use try and except to avoid the errors. for example see the code below:

>>> list_1=[1,2,3,4,5]
>>> input=raw_input("Enter the number:")
Enter the number: 5
>>> try:
...     index=list_1.index(int(input))
...     print "The index is %s" %index
... except ValueError:
...     print "The number is not in the list"
...
The index is 4

If you execute it once more time and enter the value not in the list, you will get the message which you have handled using the except.

Enter the number:7
The number is not in the list

Upvotes: 4

John La Rooy
John La Rooy

Reputation: 304215

The list comprehension using enumerate is the usual way to do something like this, but it's not that difficult using really basic concepts either.

>>> L = [4, 10, 4, 2, 9, 5, 4]
>>> L2 = []
>>> x = 4
>>> for i in range(len(L)):
...     if L[i] == x:
...         L2.append(i)
... 
>>> L2
[0, 2, 6]

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 113988

print list1.index(value) if value in list1 else "Not Found"

Upvotes: 2

RocketDonkey
RocketDonkey

Reputation: 37269

Try doing a simple check against the list (this is just a barebones example - doesn't address multiple occurrences, etc.) This uses Python's in operator and is a little more verbose than it needs to be so you can see what is happening:

>>> def SimpleCheck(my_list, num):
...   if num in my_list:
...     return l.index(num)
...   else:
...     return 'Nada'
...
>>> l = ['one', 'two', 'three']
>>> SimpleCheck(l, 'three')
2
>>> SimpleCheck(l, 'four')
'Nada'

Upvotes: 0

Related Questions