List output. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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