python find first in list code example
Example 1: find the first element in phython list
Alist = ['Sun','Mon','Tue','Wed','Thu']
print("The given list : ",Alist)
print("The first element of the list : ",Alist[0])
print("The last element of the list : ",Alist[-1])
Example 2: python get first element in a list
test_list = [1, 5, 6, 7, 4]
print ("The original list is : " + str(test_list))
res = [ test_list[0], test_list[-1] ]
print ("The first and last element of list are : " + str(res))
Example 3: python find first occurrence in list
for item in yourlist:
if item == 'value':
yourvar = item
break
Example 4: python get first occurrence in list
next(obj for obj in objs if obj.val==5)