Answers for "find in python list"

3

get all indices of a value in list python

indices = [i for i, x in enumerate(my_list) if x == "whatever"]
Posted by: Guest on March-11-2020
43

python find index by value

>>> ["foo", "bar", "baz"].index("bar")
1
Posted by: Guest on November-19-2019
22

python find in list

# There is several possible ways if "finding" things in lists.
'Checking if something is inside'
3 in [1, 2, 3] # => True
'Filtering a collection'
matches = [x for x in lst if fulfills_some_condition(x)]
matches = filter(fulfills_some_condition, lst)
matches = (x for x in lst if x > 6)
'Finding the first occurrence'
next(x for x in lst if ...)
next((x for x in lst if ...), [default value])
'Finding the location of an item'
[1,2,3].index(2) # => 1
[1,2,3,2].index(2) # => 1
[1,2,3].index(4) # => ValueError
[i for i,x in enumerate([1,2,3,2]) if x==2] # => [1, 3]
Posted by: Guest on April-10-2020
0

find item in list

def findNumber(arr, k):
    if k in arr:
        print("YES")
    else:
        print("NO")
Posted by: Guest on August-16-2021
-1

python find if part of list is in list

'''    
    check if list1 contains all elements in list2
'''
result =  all(elem in list1  for elem in list2)
if result:
    print("Yes, list1 contains all elements in list2")    
else :
    print("No, list1 does not contains all elements in list2"
Posted by: Guest on June-25-2020

Python Answers by Framework

Browse Popular Code Answers by Language