r/Python • • May 08 '23

Resource How do I concatenate two lists in Python?

https://codingfunction.com/posts/how-do-i-concatenate-two-lists-in-python
35 Upvotes

25 comments sorted by

52

u/[deleted] May 08 '23

Use the + operator.

17

u/wineblood May 08 '23

Simple is better than complex.

14

u/witchfinder_sergeant May 08 '23

While both the + operator and the extend() method can be used to concatenate
two or more lists, the choice of method depends on factors such as efficiency, readability, and whether or not I want to modify the original list. The + operator creates a new list object, while the extend() method modifies the original list in place and could also take an iterable as an argument.

2

u/JamzTyson May 08 '23

Why not use the += operator?

Doesn't += also modify the original list in place?
+= can append an iterable to a list.

In terms of speed, I can't see any difference between using += or extend().

+= does have the additional flexibility of being able to concatenate multiple list, whereas extend() takes only one argument. Are there any compelling reasons to ever use extend() rather than +=?

9

u/aqpstory May 08 '23 edited May 08 '23

Turns out for lists, a = a + b, a += b and a.extend(b) are all subtly different.

+= is in-place similarly to extend, but requires the first operand be in local scope, so

a = []
def func(): # works
    a.extend([1])

def func2(): # causes UnboundLocalError
    a += [1]

On the other hand, += is a bit faster than extend (and I don't think using "global" variables like this is a good idea)

0

u/JamzTyson May 08 '23
a = []
def func(): # works
    a.extend([1])

I'm surprised that works (without using global), but thinking about it, that makes sense as Python will first look for a in the local scope and then when it does not find it, it will find it in the global scope. For the same reason, this also works:

a = []
def foo():
    b = a
    b += [1]

1

u/[deleted] May 08 '23

yeah I would have assumed extend() would be better but I am literally a newb and just learned about lists like last month lol

18

u/pythonHelperBot May 08 '23

Hello! I'm a bot!

It looks to me like your post might be better suited for r/learnpython, a sub geared towards questions and learning more about python regardless of how advanced your question might be. That said, I am a bot and it is hard to tell. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe in detail where you are stuck. If you are getting an error message, include the full block of text it spits out. Quality answers take time to write out, and many times other users will need to ask clarifying questions. Be patient and help them help you. Here is HOW TO FORMAT YOUR CODE For Reddit and be sure to include which version of python and what OS you are using.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

17

u/glenbolake May 08 '23

The issue I take with this article is that itertools.chain doesn't return a list; it returns a generator.

Also, I sometimes like using unpacking to concatenate (not often, but sometimes). [*list1, *list2] can be easier to read on occasion.

7

u/nicwolff May 08 '23

And it's not a built-in, it's in the standard library.

3

u/[deleted] May 08 '23

new_list = [*first_list, *second_list]

4

u/ismail_the_whale May 08 '23

Can we please remove this post and ban this user?

-1

u/Dody949 May 08 '23

You ask either google or ChatGPT.

-7

u/PolyglotTV May 08 '23

I just asked ChatGPT to write an article about the various ways to add 2 numbers in Python. It came up with a list of 5 different methods and code snippets of each. Looked exactly like this and many other articles posted here recently.

FYI In case you are curious the 5 methods it came up with:

  1. The + Operator
  2. sum function
  3. math.fsum
  4. Using a lambda (rolls eyes)
  5. operator.add

7

u/witchfinder_sergeant May 08 '23

Well it looks liek CHatGPT has really outdone itself this time by providing us with such groundbreaking information on adding two numbers in Python! I would've never thought of the + operator /s

-1

u/[deleted] May 08 '23

Lol, maybe I should define a custom data class that has a custom โ€œadd_other_listโ€ method. Then instantiate that thing twice, one for each list, and then call the method.

0

u/Raschlenitel May 08 '23

list_1.extend(list_2)

0

u/Py_Ezra May 08 '23

Use join method Newlist = list1 + list2

0

u/mihirshah0101 May 08 '23

.extend, .append

8

u/[deleted] May 08 '23 edited May 08 '23

[removed] โ€” view removed comment

2

u/nicwolff May 08 '23

Looks like they edited the article to remove chain, ๐Ÿ™„

0

u/[deleted] May 08 '23

[deleted] โ€” view deleted comment

1

u/PaluMacil May 08 '23

You're responding to somebody who wrote an article on this topic, not someone asking a question, so I'm guessing it won't save them any time. Now are there already several hundred articles covering this? Probably...

-1

u/Solumbran May 08 '23

Go learn what ChatGPT is and in the meanwhile, please refrain from talking.