I want print out the position that an element is in, in the list
list = [A,B,C,D]
position=list.find("B")
print ("This is at " position " of the list")
CodePudding user response:
Find does not work for a list only a string. Use index instead, also position will need to be converted into a string to be able to print. USE THIS INSTEAD:
list = ["A","B","C","D"] # also add "" around each element
position=list.index("B") # does what find does but for a list
position = str(position) # this changes position to a string
print ("This is at " position " of the list")
CodePudding user response:
index method could be one of the options but it would only give the first occurrence. If there are multiple occurrences, then I would recommend something like this
value_list = ["A","B","C","D","A"]
occurances = [index for index, element in enumerate(value_list) if element == 'A']
#answer - [0,4]
