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

7th Apr 2020, 7:38 AM
Mandadi Praveen Kumar
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]}
7th Apr 2020, 11:05 AM
Lothar
Lothar - avatar
+ 1
Because that's how the index method work. you can take 2nd argument of index method as 1 to skip 1st in [letters]
7th Apr 2020, 7:40 AM
Tricker
+ 1
Index method returns the index no. Of first Occurrence of element searched
7th Apr 2020, 8:32 AM
ANJALI SAHU
0
much more verbosely, index() method should be named first_index_of()... conversely, index() method has not been named indexes() method ^^
7th Apr 2020, 9:08 AM
visph
visph - avatar