how to get the index of element of the iterated list directly, without using index()? How to count the instances and not values? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to get the index of element of the iterated list directly, without using index()? How to count the instances and not values?

In the "for" loop, does the program "know" the position of the iterated element in the list? I can't use index() method for getting the index because I have a bunch of identical ones and zeros, so I need the way of counting the instances of iterated elements, without checking their values. I think that maybe the "for" loop is already counting the instances or checking the next() element of the list, so I need to get to there. P.S. No, I don't wanna use i=0 ........ i+=1, I know it will work but for some reasons I will not P.P.S. No, dictionary won't work either

25th May 2018, 8:50 AM
hide on bush
hide on bush - avatar
5 Answers
+ 1
you dont need to repost. can you please provide desired input and output for your code if this doesnt help https://code.sololearn.com/caKtuDpJGjWf/?ref=app
25th May 2018, 9:17 AM
Markus Kaleton
Markus Kaleton - avatar
+ 1
yeah. in that case you could that example of mine and use if list[i] == 1: and i instead of index
25th May 2018, 3:24 PM
Markus Kaleton
Markus Kaleton - avatar
0
objects = [o1, o2, o3, o4, o5] #each object has its method, and I need to run those depending on the list I have here list = [1,0,1,1,1,0,1,0,1,1,1,0,1] for var in list: if var == 1: object_used = objects[list.index(var)] object_used.method() #obviously, this will not work because index() function returns the index of first "1" in the list, and I need to get the real position of var I'm checking
25th May 2018, 9:42 AM
hide on bush
hide on bush - avatar
0
how? tell me how to use that, cos that doesn't sound like it will work and please don't tell me it's for I in range(list): if list[i] == 1: object = objects[i]
25th May 2018, 5:22 PM
hide on bush
hide on bush - avatar
0
i need to understand how the "for" is coded
25th May 2018, 7:11 PM
hide on bush
hide on bush - avatar