if statement - Boolean. Is string in list and not last position ? Python -
i'm trying make simple line of code looks element in list , gives true or false.
i want know if it's in list . . . can position in list if it's last one, has @ position well.
examples:
my_list = ['abc','def','ghi','jkl','def'] #meets criteria my_list2 = ['abc','ghi','jkl','def'] #does not meet criteria my_search = 'def'
my_list has 'def' in middle of list , @ end meet criteria my_list2 has 'def' @ end not meet criteria
i trying along lines of,
(('def' in my_list) , (my_list[-1] == 'def')) or (('def' in my_list) , (my_list[-1] !+ 'def'))
but feel long way it.
so can check if in list minus last element:
>> my_list = ['abc','def','ghi','jkl','def'] >>> 'def' in my_list[:-1] true >>> my_list2 = ['abc','ghi','jkl','def'] >>> 'def' in my_list2[:-1] false
Comments
Post a Comment