Built with Tutify.io

    AIStudyLabs

    Add Element to Tuple in Python

    Add Element to Tuple in Python

    How to add elements to a tuple in python? Tuples in Python are similar to lists but they cannot be changed. This makes them useful in certain situations. The immutability of tuples makes them ideal for certain tasks and scenarios.

    You can add an element or multiple elements to a tuple by using many ways, for example, using the + operator, * unpacking. In this article, I will explain adding elements to a tuple by using all these methods with examples.

    <a id="h-1-quick-examples-of-add-element-to-tuple"></a>

    1. Quick Examples of Add Element to Tuple

    If you are in a hurry, below are some quick examples of how to add elements to a tuple.

    
    # Quick examples to add element to tuple
    
    # Example 1: Add two tuples
    tuple1 = (1, 3, 5)
    tuple2 = (7, 8, 9)
    tuple3 = tuple1 + tuple2
    
    # Example 2: Add tuple using list.append()
    # With one element
    tuples = (2, 4, 6, 8)
    list1 = list(tuples)
    list1.append(12)
    result = tuple(list1)
    
    # Example 3: Adding a new tuple 
    # To an existing tuple
    tuples = (3, 5, 7)
    tuples1 = tuples + (9,)
    
    # Example 4: Using tuple concatenation
    tuples2 = (1,) + tuples
    
    # Example 5: Add to a tuple with unpacking
    tuples = (2, 4, 6, 8, 12)
    tuples = (*tuples, 15)
    
    # Example 6: Unpacking the elements 
    # Of an existing tuple
    tuples = (2, 4, 6, 8, 12)
    tuples = (0, *tuples)
    
    # Example 9: Use reassignment (+=) operator  
    tuples = ('Spark', 'Python')
    tuples += ('Pandas',)
    

    <a id="h-2-add-elements-to-tuple-in-python"></a>

    2. Add Elements to Tuple in Python

    You can add elements from one tuple to another in Python by using the + operator. When you concatenate two tuples, a new tuple is created that consists of all the elements from the original two tuples. For example, let's create tuples tuple1 and tuple2, and then concatenated them using the + operator to create a new tuple tuple3.

    
    # Concatenate two tuples
    tuple1 = (1, 3, 5)
    tuple2 = (7, 8, 9)
    tuple3 = tuple1 + tuple2
    print(tuple3)
    
    # Output:
    # (1, 3, 5, 7, 8, 9)
    

    To add a tuple with a single element, you can use the + operator, just like with any other tuple. When concatenating a tuple with one element, the element needs to be in a tuple itself, otherwise, it will be interpreted as a separate value and not as part of the tuple.

    
    # Adding a new tuple 
    # To an existing tuple
    tuples = (3, 5, 7)
    tuples1 = tuples + (9,)
    print(tuples1) 
    
    # Output:
    # (3, 5, 7, 9)
    
    # Using tuple concatenation
    tuples2 = (1,) + tuples
    print(tuples2)
    
    # Output:
    # (1, 3, 5, 7)
    

    <a id="h-3-add-elements-to-tuple-using-list-append"></a>

    3. Add Elements to Tuple using list.append()

    Alternatively, you can convert the tuple to a list using the list() function, add an element to the list using the append() method, and then convert the list back to a tuple using the tuple() function.

    
    # Concatenate tuple with one element
    tuples = (2, 4, 6, 8)
    
    # Convert tuple to list
    list1 = list(tuples)
    
    # Append element
    list1.append(12)
    
    # Convert list to tuple
    result = tuple(list1)
    print(result)
    
    # Output:
    # (2, 4, 6, 8, 12)
    

    <a id="h-4-add-to-a-tuple-with-unpacking"></a>

    4. Add to a Tuple with Unpacking

    Using unpacking technique when creating a new tuple in Python, you can unpack the elements of an existing tuple into a new tuple which ideally adds elements to tuple. For example, create tuple with elements 2, 4, 6, 8, and 12. Then, created a new tuple by unpacking the elements tuples into them and adding a new element 15 at the last.

    The * operator is used to unpack the elements tuples into the new tuple. The result of the unpacking is a new tuple with the elements 2,4,6,8,12,15.

    
    # Add to a tuple with unpacking
    tuples = (2, 4, 6, 8, 12)
    tuples = (*tuples, 15)
    print(tuples)
    
    # Output:
    # (2, 4, 6, 8, 12, 15)
    
    # Unpacking the elements 
    # Of an existing tuple
    tuples = (2, 4, 6, 8, 12)
    tuples = (0, *tuples)
    print(tuples)
    
    # Output:
    # (0, 2, 4, 6, 8, 12)
    

    5. Use Reassignment (+=) Operator

    Similarly, you can also use the reassignment operator (+=) to add a new element to the same tuple.

    
    # Use reassignment (+=) operator  
    tuples = ('Spark', 'Python')
    tuples += ('Pandas',)  
    print(tuples)
    
    # Output:
    # ('Spark', 'Python', 'Pandas')
    

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained how to add elements to a tuple in python by using the + operator, * unpacking, append(), and reassignment (+=) operator with examples.

    Happy Learning !!

    Related Articles

    References