+ 1

Listbox in Python

I have read online that " insert(index,*elements) will insert one or more new lines into the listbox before the line specified by index ". I am getting the correct order in my code1(Listbox1) but not correct order in code2(Listbox2). https://code.sololearn.com/cXvqPc0HcE7b/?ref=app https://code.sololearn.com/cpV5ks7OehTT/?ref=app

27th Jun 2019, 12:59 AM
harshit
harshit - avatar
4 Réponses
+ 3
If you try to insert an element at index 4 of an empty list, it will be appended as the last (=first) element since there is no index 4. The next element that you try to insert at index 3 will be appended at the end since there is still no index 3. Only when there are enough elements in your list it will start to insert new elements at the given indices
27th Jun 2019, 5:34 AM
Anna
Anna - avatar
+ 3
harshit Actually it's exactly like I'm saying. Long story short, if you want your list to be sorted in a useful way, don't insert elements at indices that don't exist https://code.sololearn.com/cWXcb5qxuuo6/?ref=app https://code.sololearn.com/chJAAVJ3AgdI/?ref=app
27th Jun 2019, 6:08 AM
Anna
Anna - avatar
+ 1
Diego please see this.
27th Jun 2019, 5:17 AM
harshit
harshit - avatar
0
Anna When I am trying Lb.insert(4, 'Python') Lb.insert(3, 'Java') Lb.insert(2, 'C++') Lb.insert(1, 'Any other') The output is: Python Any other Java c++ which is different from what you are saying. Also after Python if we are inserting Java at index 2(3-1=2) then it should be just after Python ?
27th Jun 2019, 6:01 AM
harshit
harshit - avatar