Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9128223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:27:53+00:00 2026-06-17T07:27:53+00:00

What is the preferred way to concatenate sequences in Python 3? Right now, I’m

  • 0

What is the preferred way to concatenate sequences in Python 3?

Right now, I’m doing:

import functools
import operator

def concatenate(sequences):
    return functools.reduce(operator.add, sequences)

print(concatenate([['spam', 'eggs'], ['ham']]))
# ['spam', 'eggs', 'ham']

Needing to import two separate modules to do this seems clunky.

An alternative could be:

def concatenate(sequences):
    concatenated_sequence = []
    for sequence in sequences:
        concatenated_sequence += sequence
    return concatenated_sequence

However, this is incorrect because you don’t know that the sequences are lists.

You could do:

import copy

def concatenate(sequences):
    head, *tail = sequences
    concatenated_sequence = copy.copy(head)
    for sequence in sequences:
        concatenated_sequence += sequence
    return concatenated_sequence

But that seems horribly bug prone — a direct call to copy? (I know head.copy() works for lists and tuples, but copy isn’t part of the sequence ABC, so you can’t rely on it… what if you get handed strings?). You have to copy to prevent mutation in case you get handed a MutableSequence. Moreover, this solution forces you to unpack the entire set of sequences first. Trying again:

import copy 

def concatenate(sequences):
    iterable = iter(sequences)
    head = next(iterable)
    concatenated_sequence = copy.copy(head)
    for sequence in iterable:
        concatenated_sequence += sequence
    return concatenated_sequence

But come on… this is python! So… what is the preferred way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:27:55+00:00Added an answer on June 17, 2026 at 7:27 am

    I’d use itertools.chain.from_iterable() instead:

    import itertools
    
    def chained(sequences):
        return itertools.chain.from_iterable(sequences):
    

    or, since you tagged this with python-3.3 you could use the new yield from syntax (look ma, no imports!):

    def chained(sequences):
        for seq in sequences:
            yield from seq
    

    which both return iterators (use list() on them if you must materialize the full list). Most of the time you do not need to construct a whole new sequence from concatenated sequences, really, you just want to loop over them to process and/or search for something instead.

    Note that for strings, you should use str.join() instead of any of the techniques described either in my answer or your question:

    concatenated = ''.join(sequence_of_strings)
    

    Combined, to handle sequences fast and correct, I’d use:

    def chained(sequences):
        for seq in sequences:
            yield from seq
    
    def concatenate(sequences):
        sequences = iter(sequences)
        first = next(sequences)
        if hasattr(first, 'join'):
            return first + ''.join(sequences)
        return first + type(first)(chained(sequences))
    

    This works for tuples, lists and strings:

    >>> concatenate(['abcd', 'efgh', 'ijkl'])
    'abcdefghijkl'
    >>> concatenate([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> concatenate([(1, 2, 3), (4, 5, 6), (7, 8, 9)])
    (1, 2, 3, 4, 5, 6, 7, 8, 9)
    

    and uses the faster ''.join() for a sequence of strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which is the preferred way of defining class properties in Python and why? Is
When using git submodules, what is the preferred way of doing customizations? Should I...
I'm producing XML right from PL/SQL in Oracle. What is the preferred way of
Which one is the preferred way? Both are just number right?
Is there a preferred way of doing fine grained access that can be modified
What is the preferred way of doing the conversion using PIL/Numpy/SciPy today?
What would be your preferred way to concatenate strings from a sequence such that
Is there a preferred way to return multiple values from a C++ function? For
What is the preferred way to work with Singleton class in multithreaded environment? Suppose
What is the preferred way of deploying a compojure/sinatra applications? I have multiple sites

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.