Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Combining and Extending Tuples

00:00 When it comes to combining and extending tuples, Python supports tuple manipulations using a few different operators. The addition operator can be used to concatenate tuples, the multiplication operator can be used to multiply a tuple by an integer–basically, repeat a tuple–and the unpacking operator can be used to unpack a tuple. Note, however, that concatenation and multiplication will always result in a new tuple being created and not truly mutate the original. Onto the REPL for examples. To explore tuple concatenation, start by creating two tuples.

00:38 The first, personal_info, stores the string value “John” and the integer value 35. The second, professional_info, holds the string value “Computer science” and the string value “Python”.

00:50 You can imagine these represent records from two different database tables. Now, use concatenation to combine them into a single tuple called profile.

01:05 Examine profile and you can see by using the addition operator, the two tuples have been combined into a new tuple that stores all of the same data.

01:14 You can also use the plus-equals operator to make this an in-place operation. However, this is not truly in place and will actually create a new tuple and assign it to the original variable name.

01:26 You can see this using the built-in id() function, which returns unique IDs for individual objects in memory. Create a new tuple called profile holding the same data as the previous personal_info tuple and pass it to the id() function.

01:41 The number you see here will not be the same as the number on my screen because the ID will be based on your computer’s memory. Now you can use the plus-equals operator to add the tuple professional_info to profile.

01:58 You should now be able to verify that the two IDs are different. This shows that a new object has been created.

02:06 One last point for concatenation, you can combine two tuples using the tuple literal syntax along with the unpacking operator: profile = (*personal_info, *professional_info).

02:25 The reason this works is because the individual elements of each tuple are unpacked and become the elements of the new tuple.

02:35 So that was how to combine tuples. But how does the multiplication operator work with tuples? First, create a new tuple containing the integers 1, 2, 3.

02:46 You can multiply a tuple by some integer n and the result will be a new tuple comprising the elements of the original tuple, repeated n times. For example, numbers * 3.

02:57 The result: numbers repeated three times. Reversing the arguments works as well, so 4 * numbers will result in numbers repeated four times.

03:10 In-place multiplication is also valid and behaves similarly to in-place addition, creating a new tuple and assigning it to the same variable name. You can check using the built-in id() function once again. Like I said before, the ID on my screen will not match the one on yours.

03:28 Now, using *= in place multiply numbers by three.

03:40 The two IDs displayed are different on my screen and should also be different on yours. Again, this shows that a new object has been created. Tuple immutability holds.

03:52 Now that you’ve got these tuples with so many elements, it sure would be nice to be able to iterate over them programmatically, wouldn’t it? Yep, you guessed it. Next up, we’re talking about tuple traversal.

Become a Member to join the conversation.