0

I am a beginner in python. I am trying to assign value of a list into another list while using append or insert method. However, the output is always none. Can you please advise. Below is the piece of code:

Input:

guest_list = ['AA', 'BB', 'CC', 'DD']
new_guest_list = guest_list.append("XX")
print(new_guest_list)

Output:

None

Input:

guest_list = ['AA', 'BB', 'CC', 'DD']
new_guest_list = guest_list.insert(0,"XX")
print(new_guest_list)

Output:

None
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 3
    Possible duplicate of [Why does list.append evaluate to false?](https://stackoverflow.com/questions/1682567/why-does-list-append-evaluate-to-false) – BallpointBen Jan 09 '18 at 19:54
  • Append doesn't return anything ... Therefore new_guest _list never initialized /defined – Ankur Jyoti Phukan Jan 09 '18 at 19:55
  • For what you're trying to do (make a new `list` without modifying the old one, I assume), you'd want `new_guest_list = guest_list + ["XX"]` or `new_guest_list = `["XX"] + guest_list`, both of which keep `guest_list` unchanged and make a new `list` for `new_guest_list`. `list.append` always modifies `guest_list` in place, and never makes a copy of it (which keeps the performance acceptable; making a fresh copy for every append would make appending to large `list`s intolerably expensive). – ShadowRanger Jan 09 '18 at 20:35

2 Answers2

1
 guest_list = ['AA', 'BB', 'CC', 'DD'] 
 guest_list.append("XX") 
 print(guest_list)

 guest_list = ['AA', 'BB', 'CC', 'DD'] 
 guest_list.insert(0,"XX") 
 print(guest_list)

What you can do is first append the data to the list and then print the list itself as append does not return anything hence you are getting None as an output.If you want to assign this list to another variable then you can do this after appending data.

new_list=guest_list
print(new_list)
Demonking28
  • 749
  • 7
  • 21
  • To be clear though, `new_list=guest_list` isn't making any new `list`s at all. It's just aliasing the same `list` to a new name (in C terms, both `guest_list` and `new_list` would be pointers to the same memory address); if you change it through either name, they'll both appear to be modified (because they're both aliases to the same `list`). If you want to shallow copy the `list`, use `new_list = guest_list[:]` or `new_list = list(guest_list)` or (on Py3 only) `new_list = guest_list.copy()`. – ShadowRanger Jan 09 '18 at 20:37
  • @ShadowRanger Yep, I know that.This was in case if he wants to address the new list with a different variable, although a change in one list would affect the other. list.copy() or list.deepcopy() would be useful here. – Demonking28 Jan 09 '18 at 22:22
  • Well, not `list.deepcopy`, since that doesn't exist. :-) You need `copy.deepcopy(guest_list)` for a true deep copy. – ShadowRanger Jan 09 '18 at 22:24
0

That's not how append() works. The function adds the item to guest_list but returns nothing (None) so when you're setting new_guest_list = guest_list.append('XX') the interpreter is essentially doing the following:

1.) Add the item 'XX' to guest_list
2.) Set new_guest_list to None

If your guest_list is mutable (i.e you want it to be updated), just guest_list.append('XX') will suffice and it'll be updated. If however you want your guest_list immutable (i.e. keep it unchanged for the record), you will want to:

1.) Optional: Set your guest_list as a tuple object (note the use of regular bracket instead of square bracket), this way it will prevent accidental changes as tuple objects are immutable:

guest_list = ('AA', 'BB', 'CC', 'DD')

2.) Set your new list as a copy of your guest_list:

new_guest_list = list(guest_list)

3.) Append your new item to new list:

new_guest_list.append('XX')

The values of your guest_list and new_guest_list will be as follow:

guest_list: ('AA', 'BB', 'CC', 'DD')
new_guest_list: ['AA', 'BB', 'CC', 'DD', 'XX']
r.ook
  • 13,466
  • 2
  • 22
  • 39