Home > Back-end >  Unexpected result when using list.append(). What am I doing wrong?
Unexpected result when using list.append(). What am I doing wrong?

Time:02-08

I can't understand the following two examples of behaviour of list.append() in Python:

list_1 = ['A', 'B']
list_2 = ['C', 'D']

copy_l1 = list_1
copy_l1.append(list_2)
  1. Example

    print(copy_l1)
    

    result: ['A', 'B', ['C', 'D']]

    expected: ['A', 'B', 'C', 'D'].

    I kind of understand this, but how to get the expected result?

  2. Example

    print(list_1)
    

    result: ['A', 'B', ['C', 'D']]

    expected: ['A', 'B'].

This is the most puzzling for me. Why does copy_l1.append(list_2) also affect list_1? Due to my backgound in C, this looks to me like I'm working on pointers, but I gather that should not be the case. What means?

CodePudding user response:

The first result is expected as you are adding the list itself, not its values, to copy_l1. To get the desired result, use either of the following:

  • copy_l1 = list2
  • copy_l1.extend(list2)

The second result is harder to understand, but it has to do with the fact that lists are mutable in Python. When you use var_a = var_b, var_a points to the same variable as var_b; it is just a new name for it. But if the data type is, for example, an int or str, then if one variable is changed the other does not change. This is because these data types are immutable (unchangeable), so when you change the value, what is happening in reality is:

  1. A new variable is created
  2. Its value is set to the new value
  3. It is given the same name as the original variable - it effectively replaces it

During this process only one variable has its value changed (or rather, replaced) and the other one stays the same. You can test that the actual variable the name points to has changed using the id() function:

a = 5
b = a
print(id(a), id(b))
# Prints the same number twice
a  = 4
print(id(a), id(b))
# Prints a new number, then the old number

But because lists are mutable, they can actually be changed so this three-step process is not necessary and all variables pointing to the list get changed. You can see this using the id() function again:

a = [1, 2]
b = a
print(id(a), id(b))
# Prints two of the same number
a  = [3, 4]
print(id(a), id(b))
# Prints the same number again twice

To stop this from happening, use .copy() to get a shallow copy of the list:

copy_l1 = list1.copy()

CodePudding user response:

list_1 = ['A', 'B']
list_2 = ['C', 'D']

copy_l1 = list_1
copy_l1.extend( list_2 )
#or
copy_l1 =list_2 

CodePudding user response:

You use wrong ways of concatenating and coping lists.

To concatenate lists you can:

copy_l1.extend(list_2)
copy_l1  = list_2

To copy list you can:

copy_l1 = list_1.copy()
copy_l1 = list_1[:]
copy_l1 = list(list_1)

CodePudding user response:

  1. Use .extend() rather than .append() to concatenate lists.
copy_l1.extend(list_2)
  1. You didn't make a copy of list_1, both variables refer to the same list. Use
copy_l1 = list_1.copy()

to make a shallow copy of list_1.

  •  Tags:  
  • Related