How To Split A Tuple Into Multiple Variables In Python

Split a tuple into multiple variables in Python

Today, I want to introduce to you how to split a tuple into multiple variables in Python. As far as I know, to do this, you need to use tuple packing, get the child tuple, or use the index tuple. Read the following article and then discuss this topic together.

Split a tuple into multiple variables in Python

In Python, a tuple is a data type used to store objects that cannot be changed later as constants. Tuple has a storage method similar to the list data type.

Syntax:

tuple = ( value1, value2, valu3, ….)

Parameters

  • value1, 2, 3: Values ​​belonging to a tuple.

Note: Round brackets are used to initialize a tuple. It would be best if you were careful with sets using curly braces and lists using square brackets.

To split a tuple into multiple variables in Python, I have the following ways:

Tuple unpacking

You can split the values ​​in the value into variables by unpacking the tuple. Each variable will be counted for one value in the tuple when unpacking.

Example:

  • Initialize a tuple of 3 values.
  • Performing tuple unpacking, three variable values ​​will store three corresponding values ​​in the tuple.
myTuple = ['visit', 'learnshareit', 'website']
variable1, variable2, variable3 = myTuple

# Tuple unpacking
print('Unpacking from a tuple:')
print(variable1)
print(variable2)
print(variable3)

Output:

Unpacking from a tuple:
visit
learnshareit
website

Get the child tuple

You can split a set of values ​​into multiple variables in Python. Keep in mind the following formula to do that:

tupleName[start:end]

Parameters:

  • start: is the starting position to get the value. If left blank, the default is to get from the beginning of the tuple.
  • end: end position. If left blank, all values ​​in the tuple will be taken.

You can see it’s similar to the slice() function on strings.

Example:

  • Initialize a tuple of 4 values.
  • Break down the value by the formula.
  • You can split into as many variables as you want.
# Initialize a tuple
myTuple = ['visit', 'learnshareit', 'website', '!!']

# Split the tuple from position 0 to 2
variable1 = myTuple[0:2]

# Split the tuple from position 2 to 4
variable2 = myTuple[2:4]

print('The first variable is split from the tuple:', variable1)
print('The second variable is split from the tuple:', variable2)

Output:

The first variable is split from the tuple: ['visit', 'learnshareit']
The second variable is split from the tuple: ['website', '!!']

Split a tuple into multiple variables by index

Example:

  • Initialize a tuple of 3 values.
  • Split a tuple into multiple variables using a tuple index.
myTuple = ['visit', 'learnshareit', 'website']

variable1 = myTuple[0]
print('The first value is extracted from the tuple:', variable1)

variable2 = myTuple[1]
print('The second value is split from the tuple:', variable2)

variable3 = myTuple[2]
print('The third value is split from the tuple:', variable3)

Output:

The first value is extracted from the tuple: visit
The second value is split from the tuple: learnshareit
The third value is split from the tuple: website

Summary

Thank you for taking the time to read this article. Must you have the idea to split a tuple into multiple variables in Python? If you have an idea, congratulations. If not, you should use the tuple unpacking method. Please comment if you have any comments about the article, and we will try to answer.

Maybe you are interested:

Leave a Reply

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