Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Append Single or Multiple Elements to a Tuple in Python

  • 06 Feb, 2025
  • Com 0
Appending an element to a tuple in Python

Here are three main ways to append a single or multiple elements to a tuple in Python:

  1. Using the concatenation (+) operator (Easy way)
  2. Convert to List and Back (Efficient for large tuples)
  3. Using the += Operator (Rebinding)

Method 1: Using the concatenation (+) operator

Using the concatenation (+) operator, combine the original tuple and the tuple to be added to create a new tuple. I highly recommend this approach if you are working with a small tuple.

Adding an element at the end

If you put your new element on the right side of the + operator, it will be added at the end.

Adding an element to the tuple at the end

main_tuple = (1, 2, 4, 5)
new_element = 3

new_tuple = main_tuple + (new_element, )

print(new_tuple)

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

Adding an element at the beginning

If you put your new element on the left side of the + operator, it will append at the start of the tuple.

Adding an element to a tuple at the beginning

main_tuple = (1, 2, 4, 5)
new_element = 3

prepend_new_tuple = (new_element, ) + main_tuple
print(prepend_new_tuple)

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

Inserting in the middle

We will divide the tuple into two parts: before the desired insertion point (index of the tuple) and after the point. That will be two slices. 

Put the element to be “inserted” into its own tuple, and then concatenate all three tuples, and the final tuple has a newly added element at the center of the tuple.

main_tuple = (1, 2, 4, 5)
new_element = 3

insert_index = 2

middle_new_tuple = main_tuple[:insert_index] + \
    (new_element,) + main_tuple[insert_index:]
print(middle_new_tuple)

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

Appending multiple elements

You can append multiple elements by combining the original with a new tuple that contains multiple elements.

main_tuple = (1, 2, 4, 5)
new_tuple = (3, 6)

multi_tuple = main_tuple + new_tuple
print(multi_tuple)

# (1, 2, 4, 5, 3, 6)

Method 2: Convert a tuple to a list and back

You cannot modify a tuple, but you can modify a list. Thus, we can convert a tuple into a list, add the elements, and then convert it back to a tuple. This is helpful when dealing with complicated modifications.

For inserting multiple elements into a list, you can use a list.extend() method.

main_tuple = (1, 2, 4, 5)

main_list = list(main_tuple)

elements_to_append = [11, 12, 13, 14]

main_list.extend(elements_to_append)

main_new_tuple = tuple(main_list)

print(main_new_tuple)

# (1, 2, 4, 5, 11, 12, 13, 14)

Method 3: Using the += operator

Using the += operator on the original tuple will add new elements each time, but it will create a new variable with each iteration.

If you keep the same variable name for each iteration, you might think that it does not return a new tuple, but it does. We are just giving each tuple the same name.

It is a Shorthand for tuple = tuple + new_element.

main_tuple = (1, 2, 4, 5)

main_tuple += (3,)

print(main_tuple)

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

Select any method that suits you best.

Post Views: 58
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Print Single and Multiple Variables in Python
How to Append None to a List in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend