python - How do I find a previous element in a list? -
n = 2 list1 = [1,4,6,2,8,9,90]
how go finding number prior number n
stored above in list1
, store in variable list1_result
?
this should work:
list1 = [1,4,6,2,8,9,90] n = 2 ind = list1.index(n) list1_result = list1[ind-1] # 6
Comments
Post a Comment