How to Split a Tuple in Python?

In this tutorial, I will explain how to split a tuple in Python. Tuples are a fundamental data structure in Python, and there may be situations where you need to divide a tuple into smaller sub-tuples or groups. We’ll explore different methods to split a Python tuple.

Tuples in Python

Tuples are ordered, immutable sequences of elements enclosed in parentheses (). They allow you to store multiple values of different data types together. Here’s an example of a tuple:

employee = ("John", "Doe", 35, "New York")

In this example, we have a tuple called employee that contains four elements: a first name, last name, age, and city.

Check out Reverse a Tuple in Python

Splitting Python Tuples into Sub-Tuples

One common scenario is splitting a tuple into smaller sub-tuples of a specific size. Let’s say we have a tuple of employee data, and we want to split it into groups of three elements each. Here’s how we can achieve that:

employees = ("Alice", "Smith", 28, "Los Angeles", "Bob", "Johnson", 42, "Chicago", "Charlie", "Brown", 35, "Houston")

def split_tuple(tuple_data, n):
    return [tuple_data[i:i+n] for i in range(0, len(tuple_data), n)]

employee_groups = split_tuple(employees, 3)
print(employee_groups)

Output:

[('Alice', 'Smith', 28), ('Los Angeles', 'Bob', 'Johnson'), (42, 'Chicago', 'Charlie'), ('Brown', 35, 'Houston')]

In this example, we define a function called split_tuple that takes a tuple (tuple_data) and the desired group size (n) as parameters. It uses a list comprehension to split the tuple into sub-tuples of size n. The resulting sub-tuples are stored in the employee_groups list.

I executed the above Python code, and you can see the exact output in the screenshot below:

Split a Tuple in Python

Check out Pass a Tuple as an Argument to a Function in Python

Splitting Tuples Based on a Condition

Another scenario is splitting a tuple based on a specific condition in Python. Let’s say we have a tuple of employee data, and we want to split it into two tuples: one containing employees from New York and the other containing employees from other cities.

employees = (
    ("Emily", "Davis", 29, "New York"),
    ("Daniel", "Wilson", 38, "San Francisco"),
    ("Olivia", "Taylor", 31, "New York"),
    ("Michael", "Anderson", 45, "Boston"),
    ("Sophia", "Martinez", 27, "New York")
)

def split_tuple_by_condition(tuple_data, condition):
    true_tuple = tuple(item for item in tuple_data if condition(item))
    false_tuple = tuple(item for item in tuple_data if not condition(item))
    return true_tuple, false_tuple

new_york_employees, other_employees = split_tuple_by_condition(employees, lambda x: x[3] == "New York")
print("New York Employees:", new_york_employees)
print("Other Employees:", other_employees)

Output:

New York Employees: (('Emily', 'Davis', 29, 'New York'), ('Olivia', 'Taylor', 31, 'New York'), ('Sophia', 'Martinez', 27, 'New York'))
Other Employees: (('Daniel', 'Wilson', 38, 'San Francisco'), ('Michael', 'Anderson', 45, 'Boston'))

In this example, we define a function called split_tuple_by_condition that takes a tuple (tuple_data) and a condition function (condition) as parameters. It uses tuple comprehensions to create two new tuples: true_tuple contains elements that satisfy the condition, and false_tuple contains elements that don’t. The condition is specified using a lambda function that checks if the city (4th element) is “New York”.

Here is the exact output in the screenshot below:

How to Split a Tuple in Python

Check out How to Check if a Tuple is Empty in Python?

Splitting Tuples Using Unpacking

Python’s tuple unpacking feature allows you to split a tuple into individual variables. This can be useful when you know the structure of the tuple and want to extract specific elements. Here’s an example:

employee = ("Liam", "Johnson", 32, "Seattle", "Software Engineer")

first_name, last_name, age, city, job_title = employee
print("First Name:", first_name)
print("Last Name:", last_name)
print("Age:", age)
print("City:", city)
print("Job Title:", job_title)

Output:

First Name: Liam
Last Name: Johnson
Age: 32
City: Seattle
Job Title: Software Engineer

In this example, we have a tuple called employee that contains five elements. We use tuple unpacking to assign each element to a separate variable. This allows us to easily access and work with individual elements of the tuple.

Check out How to Print a Tuple in Python?

Splitting Tuples Inside a List

Sometimes, you may encounter a list that contains tuples, and you need to split those tuples. Here’s an example of how to handle such a scenario:

employees = [
    ("Ava", "Brown", 29, "Chicago"),
    ("Ethan", "Davis", 36, "New York"),
    ("Mia", "Wilson", 42, "Los Angeles")
]

split_employees = [(first, last, age, city.split(',')) for first, last, age, city in employees]
print(split_employees)

Output:

[('Ava', 'Brown', 29, ['Chicago']), ('Ethan', 'Davis', 36, ['New York']), ('Mia', 'Wilson', 42, ['Los Angeles'])]

In this example, we have a list called employees that contains tuples. We use a list comprehension to iterate over each tuple in the list and split the city element using the split() method. The resulting split tuples are stored in the split_employees list.

Here is an exact output in the screenshot below:

Splitting Tuples Inside a List in Python

Conclusion

In this tutorial, we explored various techniques to split tuples in Python. Whether you need to split a tuple into sub-tuples of a specific size, split based on a condition, use tuple unpacking, or handle tuples inside a list, you can do this in Python.

You may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.