• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / How to Concatenate Tuples in Python

How to Concatenate Tuples in Python

February 27, 2022 Leave a Comment

We can easily concatenate tuples in Python. To concatenate the elements of two tuples, you can use the Python + operator.

tuple1 = (1, 2, 3)
tuple2 = (4, 5)

print(tuple1 + tuple2)

#Output:
(1, 2, 3, 4, 5)

You can also use the Python sum() function to concatenate tuples.

tuple1 = (1, 2, 3)
tuple2 = (4, 5)

tuple3 = sum((tuple1,tuple2),())

#Output:
(1, 2, 3, 4, 5)

In Python, tuples are a collection of objects which are ordered and mutable. When working with a list of tuples, it can be useful to be able to concatenate and add tuples together.

We can easily add tuples together in Python.

To add two tuples together, we can use the Python + addition operator.

Just like with other collections of data in Python, + adds the elements of another tuple to the end of the first tuple.

Below is a simple Python example showing how to concatenate two tuples.

tuple1 = (1, 2, 3)
tuple2 = (4, 5)

print(tuple1 + tuple2)

#Output:
(1, 2, 3, 4, 5)

Concatenating Tuples Using Python sum() Function

Another useful function in Python is sum(). The Python sum() function takes two arguments – an iterable object which is required, and an optional starting point.

We can use the sum() function to concatenate the elements of two tuples together easily. To append the elements of one tuple to another tuple using sum(), we just pass an empty tuple to sum() for the starting value.

Below is an example in Python of how to concatenate tuples using sum().

tuple1 = (1, 2, 3)
tuple2 = (4, 5)

tuple3 = sum((tuple1,tuple2),())

#Output:
(1, 2, 3, 4, 5)

Hopefully this article has been useful for you to learn how to concatenate the elements of tuples in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Remove Commas from String
  • 2.  Using Python to Get All Combinations of Two Lists
  • 3.  Break Out of Function in Python with return Statement
  • 4.  How to Cube Numbers in Python
  • 5.  Get Current URL with Selenium in Python
  • 6.  Selenium maximize_window() Function to Maximize Window in Python
  • 7.  Python Get First Word in String
  • 8.  Multiple Condition if Statements in Python
  • 9.  Using Python to Count Number of True in List
  • 10.  Get Random Value from Dictionary in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy