0
In this ex:- letters = ['p', 'q', 'r', 's', 'p', 'u'] print(letters.index('p')) why the ouput is 0 only we it is not 4
4 Answers
+ 5
If you want to find all occurences of a character you can do something like that:
txt = 'Missi2ssi2ppi'
to_find = 'i'
ind_tbl = {}
buf = []
if txt.count(to_find) > 1:
for ind, i in enumerate(txt):
if i == to_find:
buf.append(ind)
ind_tbl.update({to_find : buf})
print(ind_tbl)
# output: {'i': [1, 4, 8, 12]}
+ 1
Because that's how the index method work.
you can take 2nd argument of index method as 1 to skip 1st in [letters]
+ 1
Index method returns the index no. Of first Occurrence of element searched
0
much more verbosely, index() method should be named first_index_of()...
conversely, index() method has not been named indexes() method ^^