patternpythonCritical
Find a value in a list
Viewed 0 times
findlistvalue
Problem
I use the following to check if
Is "
EDIT FOR REOPENING: the question has been considered duplicate, but I'm not entirely convinced: here this question is roughly "what is the most Pythonic way to find an element in a list". And the first answer to the question is really extensive in all Python ways to do this.
Whereas on the linked duplicate question and its corresponding answer, the focus is roughly only limited to the 'in' key word in Python. I think it is really limiting, compared to the current question.
And I think the answer to this current question, is more relevant and elaborated that the answer of the proposed duplicate question/answer.
item is in my_list:if item in my_list:
print("Desired item is in list")Is "
if item in my_list:" the most "pythonic" way of finding an item in a list?EDIT FOR REOPENING: the question has been considered duplicate, but I'm not entirely convinced: here this question is roughly "what is the most Pythonic way to find an element in a list". And the first answer to the question is really extensive in all Python ways to do this.
Whereas on the linked duplicate question and its corresponding answer, the focus is roughly only limited to the 'in' key word in Python. I think it is really limiting, compared to the current question.
And I think the answer to this current question, is more relevant and elaborated that the answer of the proposed duplicate question/answer.
Solution
As for your first question: "
As for your second question: There's actually several possible ways if "finding" things in lists.
Checking if something is inside
This is the use case you describe: Checking whether something is inside a list or not. As you know, you can use the
Filtering a collection
That is, finding all elements in a sequence that meet a certain condition. You can use list comprehension or generator expressions for that:
The latter will return a generator which you can imagine as a sort of lazy list that will only be built as soon as you iterate through it. By the way, the first one is exactly equivalent to
in Python 2. Here you can see higher-order functions at work. In Python 3,
Finding the first occurrence
If you only want the first thing that matches a condition (but you don't know what it is yet), it's fine to use a for loop (possibly using the
which will return the first match or raise a
Finding the location of an item
For lists, there's also the
However, note that if you have duplicates,
If there are duplicates and you want all the indexes then you can use
if item is in my_list:" is perfectly fine and should work if item equals one of the elements inside my_list. The item must exactly match an item in the list. For instance, "abc" and "ABC" do not match. Floating point values in particular may suffer from inaccuracy. For instance, 1 - 1/3 != 2/3.As for your second question: There's actually several possible ways if "finding" things in lists.
Checking if something is inside
This is the use case you describe: Checking whether something is inside a list or not. As you know, you can use the
in operator for that:3 in [1, 2, 3] # => TrueFiltering a collection
That is, finding all elements in a sequence that meet a certain condition. You can use list comprehension or generator expressions for that:
matches = [x for x in lst if fulfills_some_condition(x)]
matches = (x for x in lst if x > 6)The latter will return a generator which you can imagine as a sort of lazy list that will only be built as soon as you iterate through it. By the way, the first one is exactly equivalent to
matches = filter(fulfills_some_condition, lst)in Python 2. Here you can see higher-order functions at work. In Python 3,
filter doesn't return a list, but a generator-like object.Finding the first occurrence
If you only want the first thing that matches a condition (but you don't know what it is yet), it's fine to use a for loop (possibly using the
else clause as well, which is not really well-known). You can also usenext(x for x in lst if ...)which will return the first match or raise a
StopIteration if none is found. Alternatively, you can usenext((x for x in lst if ...), [default value])Finding the location of an item
For lists, there's also the
index method that can sometimes be useful if you want to know where a certain element is in the list:[1,2,3].index(2) # => 1
[1,2,3].index(4) # => ValueErrorHowever, note that if you have duplicates,
.index always returns the lowest index:......[1,2,3,2].index(2) # => 1If there are duplicates and you want all the indexes then you can use
enumerate() instead:[i for i,x in enumerate([1,2,3,2]) if x==2] # => [1, 3]Code Snippets
3 in [1, 2, 3] # => Truematches = [x for x in lst if fulfills_some_condition(x)]
matches = (x for x in lst if x > 6)matches = filter(fulfills_some_condition, lst)next(x for x in lst if ...)next((x for x in lst if ...), [default value])Context
Stack Overflow Q#9542738, score: 1823
Revisions (0)
No revisions yet.