+ 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?

12th Nov 2019, 12:43 AM
Stravo1
Stravo1 - avatar
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/
12th Nov 2019, 8:26 AM
Diego
Diego - avatar
+ 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.
12th Nov 2019, 12:50 AM
Dragon_King1111
Dragon_King1111 - avatar
+ 1
Dragon_King1111 I see, maybe that's the reason!! Thanks 😊! Happy Coding?
12th Nov 2019, 12:54 AM
Stravo1
Stravo1 - avatar
+ 1
Diego Thanks man 👍! This one was the proper answer I was looking for!! Happy Coding 😊!!
12th Nov 2019, 10:15 AM
Stravo1
Stravo1 - avatar