- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Questions & Answers
- Python - Online Quiz
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python - Join Tuples
Joining Tuples in Python
Joining tuples in Python refers to combining the elements of multiple tuples into a single tuple. This can be achieved using various methods, such as concatenation, list comprehension, or using built-in functions like extend() or sum().
Joining tuples does not modify the original tuples but creates a new tuple containing the combined elements.
Joining Tuples Using Concatenation ("+") Operator
The concatenation operator in Python, denoted by +, is used to join two sequences, such as strings, lists, or tuples, into a single sequence. When applied to tuples, the concatenation operator joins the elements of the two (or more) tuples to create a new tuple containing all the elements from both tuples.
We can join tuples using the concatenation operator by simply using the + symbol to concatenate them.
Example
In the following example, we are concatenating the elements of two tuples "T1" and "T2", creating a new tuple "joined_tuple" containing all the elements from both tuples −
# Two tuples to be joined T1 = (10,20,30,40) T2 = ('one', 'two', 'three', 'four') # Joining the tuples joined_tuple = T1 + T2 # Printing the joined tuple print("Joined Tuple:", joined_tuple)
Following is the output of the above code −
Joined Tuple: (10, 20, 30, 40, 'one', 'two', 'three', 'four')
Joining Tuples Using List Comprehension
List comprehension is a concise way to create lists in Python. It is used to generate new lists by applying an expression to each item in an existing iterable, such as a list, tuple, or range. The syntax for list comprehension is −
new_list = [expression for item in iterable]
This creates a new list where expression is evaluated for each item in the iterable.
We can join a tuple using list comprehension by iterating over multiple tuples and appending their elements to a new tuple.
Example
In this example, we are joining two tuples, T1 and T2, into a single tuple using list comprehension. The resulting tuple, joined_tuple, contains all elements from both T1 and T2 −
# Two tuples to be joined T1 = (36, 24, 3) T2 = (84, 5, 81) # Joining the tuples using list comprehension joined_tuple = [item for subtuple in [T1, T2] for item in subtuple] # Printing the joined tuple print("Joined Tuple:", joined_tuple)
Output of the above code is as follows −
Joined Tuple: [36, 24, 3, 84, 5, 81]
Joining Tuples Using extend() Function
The Python extend() function is used to append elements from an iterable (such as another list) to the end of the list. This function modifies the original list in place, adding the elements of the iterable to the end of the list.
The extend() function is not used for joining tuples in Python. It is used to extend a list by appending elements from another iterable (such as another list), effectively merging the two lists together.
We can join tuples using the extend() function by temporarily converting the tuples into lists, performing the joining operation as if they were lists, and then converting the resulting list back into a tuple.
Example
In the following example, we are extending the first tuple "T1" by converting it into a list "L1", then adding elements from the second tuple "T2" by first converting it into a list "L2", and finally converting the merged list back into a tuple, effectively joining the two tuples −
T1 = (10,20,30,40) T2 = ('one', 'two', 'three', 'four') L1 = list(T1) L2 = list(T2) L1.extend(L2) T1 = tuple(L1) print ("Joined Tuple:", T1)
The output obtained is as shown below −
Joined Tuple: (10, 20, 30, 40, 'one', 'two', 'three', 'four')
Join Tuples using sum() Function
In Python, the sum() function is used to add up all the elements in an iterable, such as a list, tuple, or set. It takes an iterable as its argument and returns the sum of all the elements in that iterable.
We can join a tuple using the sum() function by providing the tuple as an argument to the sum() function. However, since the sum() function is specifically designed for numeric data types, this method only works for tuples containing numeric elements. It will add up all the numeric elements in the tuple and return their sum.
Syntax
Following is the syntax for using the sum() function to join tuples in Python −
result_tuple = sum((tuple1, tuple2), ())
Here, the first argument is a tuple containing the tuples to be joined. The second argument is the starting value for the sum. Since we are joining tuples, we use an empty tuple () as the starting value.
Example
In this example, the elements of the first tuple are first appended to an empty tuple. Then elements from the second tuple are appended, resulting in a new tuple that is a concatenation of the two −
T1 = (10,20,30,40) T2 = ('one', 'two', 'three', 'four') T3 = sum((T1, T2), ()) print ("Joined Tuple:", T3)
After executing the above code, we get the following output −
Joined Tuple: (10, 20, 30, 40, 'one', 'two', 'three', 'four')
Joining Tuples using for Loop
A for loop in Python is used for iterating over a sequence (such as a list, tuple, string, or range) and executing a block of code for each element in the sequence. The loop continues until all elements have been processed.
We can join a tuple using a for loop by iterating over the elements of one tuple and appending each element to another tuple with the "+=" operator.
Example
In the following example, we are iterating over each element in tuple T2, and for each element, we are appending it to tuple T1, effectively joining the two tuples −
T1 = (10,20,30,40) T2 = ('one', 'two', 'three', 'four') for t in T2: T1+=(t,) print (T1)
We get the output as shown below −
(10, 20, 30, 40, 'one', 'two', 'three', 'four')