0

I just found out that you can do this

objecttoaddto = [1,2,3,4]
objecttoaddto +=[5,6]

Which is simpler than

objecttoaddto = [1,2,3,4]
objecttoaddto.append(5)
objecttoaddto.append(6)

Especially if I want to add multiple elements/objects.

Why would I ever want to use the method .append() instead of just += ?

PixelSnader
  • 552
  • 1
  • 4
  • 9
  • You use it when you only have one item to add, rather than a list. – Barmar Jul 14 '20 at 19:54
  • 1
    FYI, those are lists, not arrays. – Barmar Jul 14 '20 at 19:55
  • @Barmar But why would I want a specific way to add only one item, if there is already a way to add one or multiple? That's just duplicate functionality, and it doesn't even save me any character/typing. – PixelSnader Jul 14 '20 at 19:59
  • If you only want to add 1 item, you would have to first put it into a list, then concatenate the lists. That list is unnecessary. – Barmar Jul 14 '20 at 20:00
  • 1
    Adding 1 item at a time is much more common than concatenating lists. – Barmar Jul 14 '20 at 20:00
  • += should be faster than multiple append. But there are cases when you have your items one at a time and need to append nonetheless – Ehsan Jul 14 '20 at 23:49

0 Answers0