+ 25

Problem With Python Lists and Object Reference

Till I know, virtually every item of data in a Python program is an object of a specific type or class. While performing m=300, the assignment to a variable creates an integer object with the value 300 and assigns the variable n to point to that object. If n=300 is also performed, both point to the same object. Sample Code : https://code.sololearn.com/cTW7Fyo7F0Eq/#py Now I saw some similar thing happening with lists. Same elements in a list point to the same object and in fact have a same index position. Sample Code : https://code.sololearn.com/cMvbNEvCfoGu/#py While I was performing a program in which all elements at even index location needs to be removed, I noticed that the 2nd index isn't present in the list. Index 1 and 2 have the same value i.e. 1 and both have the same index location i.e.1 and the element after it has an index value of 3. If I want to remove the element at 2nd index position then it's not possible here. Code : https://code.sololearn.com/c6F66H9v2nBz/#py Can anyone help ? How to solve this issue ?

12th Sep 2019, 12:41 PM
Nova
Nova - avatar
7 Answers
+ 11
Ohh I noticed it now that when I am removing elements from the list, the size of the list is also decreasing. That's the reason for the error being occured. HonFu, iterating from back is a great idea. Thanks For Your suggestion, I'll use words as the variable names that will completely describe the variables. I didn't thought the code was not readable from your perspective. I really feel bored to write too long names of variables again and again. But from now I'll try to use complete words. ~ swim ~, your suggestion of using while loop is also useful for me. Thanks for explaining the concept of object identity. So, Thanking to both of you for your valuable suggestions and making me understand my mistakes 😇👍
12th Sep 2019, 3:31 PM
Nova
Nova - avatar
+ 10
HonFu I got your point. The code should be readable even if I read it after a couple of months. I should understand what actually I had coded and the complete words as variable names are going to be helpful for proper understanding. The second example makes it clear what the program exactly does. Comments too do help to understand the code well. Thanks For Your Suggestions đŸ€“đŸ™
13th Sep 2019, 8:37 AM
Nova
Nova - avatar
+ 4
Looping over an iterable while you change it is always risky and messy. Best - if possible - is to loop over a copy of the list: for element in your_list[:]: # remove stuff from actual list If you want to do it anyway, you can prevent trouble by iterating from the back: for i in range(len(arr)-1, - 1, - 1): if not i%2: del arr[i] In simple cases you can use list comprehension: arr = [arr[i] for i in range(0, len(arr), 2] I would say something specific about your code, but I find it hard to read. You should consider switching to real variable names instead of just letters - complete words or even several words per name that clearly describe what the variable is. That makes your code a lot more readable for outsiders - or for yourself a few weeks from now.
12th Sep 2019, 1:18 PM
HonFu
HonFu - avatar
+ 3
Nova, I understand that it can be annoying to write long variable names. Your editor will allow you not to write them out every time, but it makes a line of code too long to read it conveniently sometimes. And if you want to keep your code readable on a smartphone, like for Sololearn, the lines could well be even shorter. However, understanding the code is more important - also for yourself. Even if the algorithm is clear to you now, in a few weeks, when you look at it again, it will look to you as if a different person wrote it. Imagine a vocabulary trainer, primitive example. What is better to grasp: for x in y: if input() == x: y.pop(x) or... for word in vocabulary: if input() == word: vocabulary.pop(word) Even if you have no idea what the code is even about, you'll get an idea what it does. It's almost English, really. In cases, where names alone don't suffice to explain your code, you may sparsely add comments.
12th Sep 2019, 5:08 PM
HonFu
HonFu - avatar
0
hi
19th Sep 2019, 9:10 AM
BHARATH 3799
BHARATH 3799 - avatar