• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Split Tuple into Multiple Variables

Python Split Tuple into Multiple Variables

February 25, 2022 Leave a Comment

In Python, you can split tuples into multiple variables with tuple unpacking. You can unpack tuples with variable names separated by commas.

x, y, z = (0, 1, 2)

print(x)
print(y)
print(z)

#Output:
0
1
2

In Python, tuples are a collection of objects which are ordered and mutable. When working with tuples, it can be useful to be able to unpack the values of the tuples and create new variables.

You can unpack tuples with variable names separated by commas.

To split a tuple, just list the variable names separated by commas on the left-hand side of an equals sign, and then a tuple on the right-hand side.

Below is an example of splitting a tuple into three variables in Python.

x, y, z = (0, 1, 2)

print(x)
print(y)
print(z)

#Output:
0
1
2

Using Tuple Unpacking to Swap Values in Python

One application of splitting a tuple is when you want to efficiently swap the value of two variables in Python. We can swap values with tuple unpacking in the following way.

x = 2
y = 3

x, y = y, x     #equivalent to x, y = (y, x)

print(x)
print(y)

#Output:
3
2

Splitting a List of Tuples into Lists in Python

When working with lists of tuples, sometimes it can be useful to able to split the list of tuples into lists containing the values of each tuple element.

We can use tuple unpacking to create new lists from lists of tuples in a similar way as we can create variables from tuples.

Below is an example of how to split a list of tuples into lists using Python.

list_of_tuples = [(0,1),(1,2),(2,3),(3,4)]

def splitListOfTuples(lst):
    lst1 = []
    lst2 = []
    for x, y in lst:
        lst1.append(x)
        lst2.append(y)
    return (lst1, lst2)

x, y = splitListOfTuples(list_of_tuples)

print(x)
print(y)

#Output:
[0, 1, 2, 3]
[1, 2, 3, 4]

Hopefully this article has been useful for you to understand how to split a tuple into multiple variables using Python.

Other Articles You'll Also Like:

  • 1.  Python power function – Exponentiate Numbers with math.pow()
  • 2.  Using Python to Check If List of Words in String
  • 3.  Golden Ratio Constant phi in Python
  • 4.  Using Python to Add Items to Set
  • 5.  Get First Digit in Number Using Python
  • 6.  How to Create Block and Multi-Line Comments in Python
  • 7.  How to Check if Number is Divisible by 3 in Python
  • 8.  Add Seconds to Datetime Variable Using Python timedelta() Function
  • 9.  Using Python to Split String by Tab
  • 10.  How to Iterate over Everything in Word Document using python-docx

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy