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

The Programming Expert

Solving All of Your Programming Headaches

  • 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 Non-Empty Directory
  • 2.  Pythagorean Theorem in Python – Calculating Length of Triangle Sides
  • 3.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 4.  Sign Function in Python – Get Sign of Number
  • 5.  Using readlines() and strip() to Remove Spaces and \n from File in Python
  • 6.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 7.  Truncate String in Python with Slicing
  • 8.  How to Sort Numbers in Python Without Sort Function
  • 9.  Get Day of Year from Date in pandas DataFrame
  • 10.  for char in string – How to Loop Over Characters of String 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