
Let’s assume that we have three different tuples containing the personal details of four customers. To learn more about zipping tuples, consider the following example. The zip() method takes multiple sequence objects and returns an iterable object by matching their elements. Moreover, multiplying a tuple by an integer produces a tuple containing the original tuple repeated that many times.
#Tuple unpacking python code#
For example, the following code concatenates two tuples: tuple_1 = (1, 2)

To concatenate two or more tuples, we can use + sign as with strings. If you’re not familiar with indexing and slicing a sequence object in Python or would like to review, there is a good tutorial on the Dataquest blog at Tutorial: Demystifying Python Lists. The subsequent statements print out different slices of the tuple, as shown. Although it’s unusual, it’s another way to declare a tuple by providing a sequence of objects separated by commas without enclosing them in parentheses. The first line of the code declares a tuple of integer values. Let’s take a look at some examples: num_tuple = 2, 4, 5, 7, 8, 10 To get a range of elements within a tuple, we can specify a range of indices to retrieve by selecting where to start (inclusive) and end (exclusive) the range. Slicing a tuple is as simple as slicing a Python string or a list with the same rules.
#Tuple unpacking python series#
This means that we can access an element stored in an inner tuple (a tuple stored inside another tuple) by doing a series of indexing operations.
#Tuple unpacking python how to#
The last statement prints out the data type of the second element of the tuple, which is a list object.įurthermore, the following piece of code shows how to retrieve the second element of the inner tuple, at index 2. In the code above, the first and second statements return the values of the tuple’s first and last elements. Print('The data type of the second element:', type(a_tuple)) The first element: 0 Let’s try indexing a tuple: print('The first element:', a_tuple) As with strings, the index of the first element is 0, the second element is 1, and so on. Indexing and Slicing a TupleĪs mentioned earlier, because a tuple is a sequence of objects, we can access these objects through indexing. The trailing comma means that the parentheses are holding a single-value tuple, not to increase the precedence of a mathematical operation. We need to add an extra comma just before the closing parenthesis, like this: So, how can we declare a single-value tuple? The answer is easy. Let’s look at its data type: print(type(('A'))) The following code returns the entire tuple and its data type. The code above creates a tuple containing an integer, a list, a tuple, and a float number.


A tuple is an immutable object, which means it cannot be changed, and we use it to represent fixed collections of items. If you’re not familiar with these or would like to review, you might like to try our Python Basics for Data Analysis – Dataquest.Ī tuple represents a sequence of any objects separated by commas and enclosed in parentheses. In this tutorial, we assume you know the fundamentals of Python, including variables, data types, and basic structures.
