Skip to main content
Post Closed as "Duplicate" by Jason Sperske, CommunityBot, DNA, Martijn Pieters, plannapus
edited tags
Link
Lev Levitsky
  • 66.4k
  • 23
  • 155
  • 184
Source Link
Manny_G
  • 333
  • 4
  • 11

difference between adding lists in python with + and +=

I've noticed when experimenting with lists that p= p+i is different then p += i For example:

test = [0, 1, 2, 3,] p = test test1 = [8] p = p + test1 print test 

In the above code test prints out to the original value of [0, 1, 2, 3,]

But if I switch p = p + test1 with p += test1 As in the following

test = [0, 1, 2, 3,] p = test test1 = [8] p += test1 print test 

test now equals [0, 1, 2, 3, 8]

What is the reason for the different value?