0

[Solved] Python, append() and NoneType, no slice: is there a hack around it, if there is which is preferred(append or +)?

I tried to use append() on a list only to realize I couldn't slice it because it was none type. I thought append only altered the list content. I am bewildered by what I did wrong because I wasn't able to print it even, after appending it. So I did a search, saw on StackOverflow that list + list was better, kept the list pure and all. Is that really a general statement of truth? What are your opinions on it? When do you refrain from using +, I mean, is there a situation where append is better? I might need to review my previous assignments depending on what you say. Thank you. I was doing a base conversion exercise. Here you can see how I had to use +. I cannot even tell if I forgot doing something with append that I used to do in my earlier exercises. I could also use some advice on concise and precise commenting. I suck at it. https://code.sololearn.com/c9l8zpAzr0VE/?ref=app

24th Apr 2022, 8:26 AM
Korkunç el Gato
Korkunç el Gato - avatar
7 Réponses
+ 5
Korkunç TheTerrible , here is a test about the execution time of various ways to add elements to a list: https://code.sololearn.com/cxU4i0oZOitk/?ref=app
24th Apr 2022, 3:35 PM
Lothar
Lothar - avatar
+ 3
Korkunç TheTerrible , the dec_to_bin() function does not return the correct bin string. see your code slightly modified: (to check if your code is working properly, you can use the built-in function bin(...), which expects an integer value and returns the binary string) https://code.sololearn.com/cmgPq9A1F61v/?ref=app
24th Apr 2022, 10:47 AM
Lothar
Lothar - avatar
+ 1
Lothar Hi, thank you for the reply: - so with each iteration I insert str(r) number at index 0, right? That's great, thank you. I had tried to get rid of slicing because of one of your posts about that earlier yet in another exercise of mine, this answers that too. <nonsense> This place had nonsense in it because my eyes failed to see things that they should have, sparing other newbies not to cause confusion. </nonsense>
24th Apr 2022, 11:24 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
BTW, if you have time, what about a comparison between + and append() and in what situations append() is preferred wrt to "+", if ever? That's my original question, this code is just an example to how I had to use an operator rather than a method. Kind of makes sense. What I don't understand is why methods return None, when I'm printing an object using the method on it. Why isn't it the same object, only altered? I need to keep rereading things I guess.
24th Apr 2022, 11:36 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Lothar I was wrong, the code works so I deleted my post. My number was 2517 not 2157. Sorry for that. (one of the causes of the nonsense) <nonsense>Still though, what I did earlier works. So because I got lucky here, I don't understand what the warning is. (if a newbie's reading this: this paragraph, too, is nonsense, due to misreading something twice since my first testing) </nonsense> I will study your code, I feel I took enough of your time. Thanks a lot, btw :-)
24th Apr 2022, 11:41 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Lothar I apologize for pinging your name many times, I understand it all now. Thank you so much. I wasn't lucky, I was blind. I didn't realize the test number gave the wrong result(lol I managed to not see it during my recheck) I will keep this question as unsolved, I wonder what people have to say about the title question BUT thank you, and please don't hate me for @'ting so frequently. I hate it when I recheck and misread something again. I'll do more checks.
24th Apr 2022, 11:56 AM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Today is my swoosh day. Now what I'm wondering is how come the x = x // 2 followed by r = x % 2 didn't reduce the number of operations by half, skipping every other remainder not once, but always. I am also afraid of committing another blindness with supposedly functional eyes. (I think it's rather my brain that skips things that are in order, inadverently, some dyslexia sort of thing) I don't want to annoy all these lovely people who help us noobs so much, so I will not ask more about this to anyone. I will rest a little and examine it until I understand why my code doesn't fail even more. And I am overstating, yes, but if anyone sees this, I really want to know, as stated in my title question, where append alters a list more favorably thab the plus operator does. Why append is used at all. Sorry to all my noobmates for any confusion I might have caused. This sort of compulsion is bad, I know.
24th Apr 2022, 12:59 PM
Korkunç el Gato
Korkunç el Gato - avatar