23

Can this be reduced to a single line (after assigning a)?

a = [1,2,3]
b = a[:]
b.append(4)
iTayb
  • 12,373
  • 24
  • 81
  • 135
user1002973
  • 2,088
  • 6
  • 22
  • 31

1 Answers1

53

The following is probably the simplest:

b = a + [4]

Here, you don't need a[:] since we're no longer copying the reference (+ creates and returns a new list anyway).

NPE
  • 486,780
  • 108
  • 951
  • 1,012