swap tuple elements in python

Swap Elements in a Python Tuple

In this tutorial, we will look at how to swap two elements in a tuple in Python with the help of some examples.

Can you swap tuple elements?

No. Tuples are an immutable data structure in Python and thus cannot be modified after they are created. You can, however, create a new tuple with the required elements swapped.

If you prefer a video tutorial over text, check out the following video detailing the steps in this tutorial –

How to create a new tuple with elements swapped?

If you want to swap elements in a tuple, use the following steps –

  1. Create a list from the tuple elements.
  2. Swap the relevant elements in the list using their index.
  3. Create a new tuple from the list elements.

This will result in a new tuple with the required elements swapped from the original tuple.

Let’s now look at some examples of using this method.

Examples

Let’s create a tuple with three elements – 1, 2, and 3 and then use the above method to swap elements 2 and 3 and get a new tuple.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# create a tuple
t = (1, 2, 3)
# convert the tuple to list
ls = list(t)
# swap the elements in the list using their index
ls[1], ls[2] = ls[2], ls[1]
# create a new tuple from the list elements
new_t = tuple(ls)
# print the new tuple
print(new_t)

Output:

(1, 3, 2)

You can see that the required elements are swapped in the resulting tuple.

Let’s look at another example. This time we will swap the first two elements of a tuple with string values.

# create a tuple
t = ('Jim', 'Ben', 'Tom', 'Zack')
# convert the tuple to list
ls = list(t)
# swap the elements in the list using their index
ls[0], ls[1] = ls[1], ls[0]
# create a new tuple from the list elements
new_t = tuple(ls)
# print the new tuple
print(new_t)

Output:

('Ben', 'Jim', 'Tom', 'Zack')

The resulting tuple has the first two elements swapped from the original tuple.

FAQs

Can you swap two elements in a Python Tuple?

No. Tuples are immutable and thus cannot be modified after creation. You can create a new tuple with the elements swapped.

How to create a new tuple resulting from swapping two elements in the original tuple?

Create a list from the original tuple values, swap the relevant elements in the list using their index, and create a tuple from the list elements using the tuple() function.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top