python where in list code example
Example 1: python find in list
'Checking if something is inside'
3 in [1, 2, 3]
'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,2,3,2].index(2)
[1,2,3].index(4)
[i for i,x in enumerate([1,2,3,2]) if x==2]
Example 2: python list
myList = [9, 'hello', 2, 'python']
print(myList[0])
print(myList[-3])
print(myList[:3])
print(myList)
Example 3: python lists
games = ['Fortnite', 'CS:GO', 'Temple Run']
print(games)
Example 4: lists in python
[0,'',True,5.2,False,[1,2]]