+ 1
List output.
Hi guys, In the following code, the program outputs the list like this [1, 3, 5, 7]. Why did remove(i) method remove 0,2,4,6 ? Could you have an idea? Thanks! https://code.sololearn.com/cOoqj7PySBij/?ref=app
6 Respuestas
+ 2
</€N!GM@>🏴☠️ , remove method takes index and removes array element at that index. Your code runs like this:
Initial Array: [0,1,2,3,4,5,6,7,8]
During 1st loop,i = 0 and element at ith position is removed (0)
Array: [1,2,3,4,5,6,7,8]
During 2nd loop,i = 1 and element at ith position is removed (2)
Array: [1,3,4,5,6,7,8]
During 3rd loop,i = 2 and element at ith position is removed (4)
Array: [1,3,5,6,7,8]
This continues and you get your final answer as you told [1, 3, 5, 7].
+ 1
Lets take a look at second loop)
First itterate removes entry №0 it is 0
Second itterate removes entry №1 it is 2 (because now it contents only [1,2,3,4,5,6]
Third removes entry №3 it 4
Fourth removes №4 it is remaining 6
+ 1
If you want to remove each entry try second loop as
For i in len(myList):
+ 1
I got it guys 👍🏻
Alex Vernee
Bhavya Ruparelia
Thank you so much!
0
</€N!GM@>🏴☠️ , if you print array directly then it will be printed as you have told. To print only elements without [], you have to use loop or you can write:
print(" ".join(array_name))
Here join function converts whole array in single line string seperated with space.
0
Bhavya Ruparelia
I’ve updated my question.
Thanks for your answer.