Python add tuples to tuple python

How to concatenate tuples in Python [9 different ways]

In this python tutorial, we will discuss how to concatenate tuples in Python using different ways.

There are nine ways to concatenate tuples in Python, which are shown below:

  • Using the + operator
  • Using the * operator
  • Using the += operator
  • Using the extend() method
  • Using the itertools.chain() function
  • Using the tuple() constructor
  • Using slicing and the + operator
  • Using sum() function

Python concatenate tuples

In Python, tuples are immutable sequences of values, which means you cannot change the contents of a tuple once it is created. However, you can concatenate two or more tuples to create a new tuple that contains all the elements from the original tuples.

Method-1: Python concatenate tuples using the + operator

We can use the + operator to concatenate two or more tuples.

# Create two tuples named 'my_tuple1' and 'my_tuple2' with three and two elements, respectively my_tuple1 = ('New York', 'Texas', 'Houston') my_tuple2 = ('Chicago', 'California') # Concatenate the two tuples using the '+' operator and store the result in a new tuple named 'res' res = my_tuple1 + my_tuple2 # Print the resulting tuple 'res' to the console print(res)

The above code creates two tuples, my_tuple1, and my_tuple2, with three and two elements respectively.

  • It then concatenates the two tuples using the + operator and stores the result in a new tuple called res. Finally, the res tuple is printed to the console using the print() function.
Читайте также:  File data type in php

Python concatenate tuples

Method-2 Python concatenate tuples using the * operator

You can also use the * operator to concatenate multiple copies of a tuple.

# Create a tuple named 'tuple1' with three string elements tuple1 = ('USA', 'United Kingdom', 'Canada') # Create a new tuple named 'tuple2' by multiplying 'tuple1' by 2 tuple2 = tuple1 * 2 # Print the resulting tuple 'tuple2' to the console print(tuple2) 

The above code creates a tuple named tuple1 with three string elements.

  • It then creates a new tuple named tuple2 by multiplying tuple1 by 2. The resulting tuple tuple2 will contain two copies of the original tuple1. Finally, the tuple2 tuple is printed to the console using the print() function.
Output: ('USA', 'United Kingdom', 'Canada', 'USA', 'United Kingdom', 'Canada')

Method-3: Python concatenate tuples using the += operator

You can use the += operator to concatenate a tuple to an existing tuple.

# Create a tuple named 'tuple1' with three string elements tuple1 = ('USA', 'United Kingdom', 'Brazil') # Create a new tuple named 'tuple2' with three integer elements tuple2 = (4, 5, 6) # Concatenate 'tuple1' and 'tuple2' using the '+=' operator, and store the result in 'tuple1' tuple1 += tuple2 # Print the resulting tuple 'tuple1' to the console print(tuple1) 

The above code creates two tuples, tuple1 with three string elements, and tuple2 with three integer elements.

  • It then concatenates tuple2 to tuple1 using the += operator, which modifies tuple1 in place by adding the elements of tuple2 to the end of it. Finally, it prints the resulting tuple1 tuple to the console.
Output: ('USA', 'United Kingdom', 'Brazil', 4, 5, 6)

Method-4: Python concatenate tuples using the extend() method

You can use the extend() method to append elements from one tuple to another.

# Create a tuple named 'tuple1' with three string elements tuple1 = ('USA', 'United Kingdom', 'Brazil') # Create another tuple named 'tuple2' with three integer elements tuple2 = (4, 5, 6) # Convert 'tuple1' and 'tuple2' to lists named 'list1' and 'list2', respectively list1 = list(tuple1) list2 = list(tuple2) # Extend 'list1' with the elements of 'list2' using the 'extend()' method list1.extend(list2) # Convert the extended 'list1' back into a tuple named 'tuple3' tuple3 = tuple(list1) # Print the resulting tuple 'tuple3' to the console print(tuple3) 

The above code creates two tuples, tuple1, and tuple2, with string and integer elements respectively.

  • It then converts these tuples to lists and uses the extend() method to add the elements of tuple2 to list1. The resulting list is then converted back into a tuple named tuple3, which is printed to the console.
Output: ('USA', 'United Kingdom', 'Brazil', 4, 5, 6)

Method-5: Python concatenate tuples using the itertools.chain() function

You can use the itertools.chain() function to concatenate two or more tuples

#The itertools module is imported. import itertools #The code creates three tuples, tuple1, tuple2, and tuple3, each containing some elements. tuple1 = ('USA', 'United Kingdom', 'Brazil') tuple2 = (4, 5, 6) tuple3 = (7, 8, 9) #itertools.chain() function is then used to concatenate three tuples into a single iterable #object. #The resulting iterable is converted to a tuple using the tuple() constructor and assigned to #the variable tuple4 tuple4 = tuple(itertools.chain(tuple1, tuple2, tuple3)) #tuple4 is printed to the console. print(tuple4) 

The above code uses the itertools module to concatenate three tuples tuple1, tuple2, and tuple3 into a single tuple tuple4.

  • The itertools.chain() function is used to create an iterable that chains all the tuples, and then the iterable is converted to a tuple using the tuple() constructor. The resulting tuple is assigned to the variable tuple4 and printed to the console.
Output: ('USA', 'United Kingdom', 'Brazil', 4, 5, 6, 7, 8, 9)

Method-6: Python concatenate tuples using the tuple() constructor

You can use the tuple() constructor to concatenate two or more tuples.

#Define two tuples named tuple1 and tuple2 tuple1 = ('USA', 'United Kingdom', 'Brazil') tuple2 = ('Chicago', 'Los Angeles') #Concatenate tuple1 and tuple2 using the + operator #Convert the concatenated tuple into another tuple using the tuple() function tuple3 = tuple(tuple1 + tuple2) #Print the final tuple named tuple3 print(tuple3)

The above code defines two tuples tuple1 and tuple2 containing country and city names as elements.

  • It then concatenates the two tuples using the + operator and converts the resulting concatenated tuple to a new tuple tuple3 using the tuple() constructor. Finally, it prints the contents of tuple3 to the console.
Output: ('USA', 'United Kingdom', 'Brazil', 'Chicago', 'Los Angeles')

Method-7: Python concatenate tuples using slicing and the + operator

You can use slicing and the + operator to concatenate two or more tuples.

#Define three tuples named tuple1, tuple2 and tuple3 tuple1 = ('USA', 'United Kingdom', 'Brazil') tuple2 = ('Chicago', 'Los Angeles') tuple3 = ('Australia', 'San Diego', 'Las Vegas') # using slicing and + operator to concatenate these tuples tuple4 = tuple1[:2] + tuple2 + tuple3[1:] #Printing tuple4 on the console print(tuple4) 

The above code creates three tuples tuple1, tuple2, and tuple3, each containing a list of items.

  • tuple1 contains the names of three countries, tuple2 contains the names of two cities in the United States, and tuple3 contains the names of three cities in different parts of the world.
  • The fourth tuple tuple4 is created by concatenating the first two items of tuple1 with tuple2, and then appending the second and third items of tuple3. The resulting tuple contains a total of six items, including the cities and countries listed in the previous tuples.
  • The code then prints out tuple4 using the print() function.
Output: ('USA', 'United Kingdom', 'Chicago', 'Los Angeles', 'San Diego', 'Las Vegas')

Method-8: Python concatenate tuples using sum() function

You can also use the built-in sum() function to concatenate two or more tuples in Python.

#Define three tuples named tuple1, tuple2 and tuple3 tuple1 = ('USA', 'United Kingdom', 'Brazil') tuple2 = ('Chicago', 'Los Angeles') tuple3 = ('Australia', 'San Diego', 'Las Vegas') #The sum() function is used to concatenate the three tuples and return a single tuple. #An empty tuple is provided as the second argument to sum() to start the summing process #with an empty tuple. tuple4 = tuple(sum((tuple1, tuple2, tuple3), ())) #Printing tuple4 on the console print(tuple4)

The above code creates three tuples tuple1, tuple2, and tuple3 each containing a list of items.

  • The sum() function is used to concatenate these three tuples, and the resulting tuple is stored in tuple4. An empty tuple is provided as the second argument to sum() to start the summing process with an empty tuple.
  • The sum() function treats each tuple as a sequence and concatenates them into a single sequence. Finally, the tuple() function is used to convert the resulting sequence into a tuple.
  • The resulting tuple tuple4 contains a total of eight items, including the cities and countries listed in the previous tuples. The code then prints out tuple4 using the print() function.
Output:('USA', 'United Kingdom', 'Brazil', 'Chicago', 'Los Angeles', 'Australia', 'San Diego', 'Las Vegas')

Conclusion

In this Python tutorial, we have covered how to concatenate tuples using the following methods:

  • Using the + operator
  • Using the * operator
  • Using the += operator
  • Using the extend() method
  • Using the itertools.chain() function
  • Using the tuple() constructor
  • Using slicing and the + operator
  • Using sum() function

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Источник

Python — Update Tuples

Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created.

But there are some workarounds.

Change Tuple Values

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.

Example

Convert the tuple into a list to be able to change it:

x = («apple», «banana», «cherry»)
y = list(x)
y[1] = «kiwi»
x = tuple(y)

Add Items

Since tuples are immutable, they do not have a built-in append() method, but there are other ways to add items to a tuple.

1. Convert into a list: Just like the workaround for changing a tuple, you can convert it into a list, add your item(s), and convert it back into a tuple.

Example

Convert the tuple into a list, add «orange», and convert it back into a tuple:

2. Add tuple to a tuple. You are allowed to add tuples to tuples, so if you want to add one item, (or many), create a new tuple with the item(s), and add it to the existing tuple:

Example

Create a new tuple with the value «orange», and add that tuple:

thistuple = («apple», «banana», «cherry»)
y = («orange»,)
thistuple += y

Note: When creating a tuple with only one item, remember to include a comma after the item, otherwise it will not be identified as a tuple.

Remove Items

Note: You cannot remove items in a tuple.

Tuples are unchangeable, so you cannot remove items from it, but you can use the same workaround as we used for changing and adding tuple items:

Example

Convert the tuple into a list, remove «apple», and convert it back into a tuple:

Or you can delete the tuple completely:

Example

The del keyword can delete the tuple completely:

thistuple = («apple», «banana», «cherry»)
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists

Источник

Оцените статью