+ 1
Why is this happening?
This works (but the code below this one doesn't): >>>x = list("1234") >>>x >>>['1', '2', '3', '4'] >>>x.append("5") >>>x >>>['1', '2', '3', '4', '5'] But if I directly do: >>>x=list("1234").append("5") Then this happens: >>>type(x) <class 'NoneType'> Why? Workaround of this problem: >>>x=list("1234")+["5"] >>>x >>>['1', '2', '3', '4', '5'] How do they all differ and why?
4 Antworten
+ 3
The append() method modifies the existing. It doesn't return a new list. In fact, it returns None.
x = list("1234").append("5")
print(x) # None
https://stackoverflow.com/questions/3840784/
+ 2
Its something like this:
if you do x("1234").append("5") its like youre creating the list while also appending 5, which might cause an error. First creating the list helps.
+ 1
Dragon_King1111 I see, maybe that's the reason!!
Thanks 😊!
Happy Coding?
+ 1
Diego Thanks man 👍! This one was the proper answer I was looking for!!
Happy Coding 😊!!