+ 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

13th Oct 2020, 3:42 AM
</€N!GM@>🏴‍☠️
</€N!GM@>🏴‍☠️ - avatar
6 ответов
+ 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].
13th Oct 2020, 4:31 AM
Bhavya Ruparelia
Bhavya Ruparelia - avatar
+ 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
13th Oct 2020, 4:24 AM
Alex Vernee
Alex Vernee - avatar
+ 1
If you want to remove each entry try second loop as For i in len(myList):
13th Oct 2020, 4:26 AM
Alex Vernee
Alex Vernee - avatar
+ 1
I got it guys 👍🏻 Alex Vernee Bhavya Ruparelia Thank you so much!
13th Oct 2020, 4:41 AM
</€N!GM@>🏴‍☠️
</€N!GM@>🏴‍☠️ - avatar
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.
13th Oct 2020, 4:04 AM
Bhavya Ruparelia
Bhavya Ruparelia - avatar
0
Bhavya Ruparelia I’ve updated my question. Thanks for your answer.
13th Oct 2020, 4:18 AM
</€N!GM@>🏴‍☠️
</€N!GM@>🏴‍☠️ - avatar