Defining Tuples
In this lesson, you’ll explore defining and using tuples. Tuples are identical to lists in all respets, except two. They are immutable and are defined by enclosing the elements in parentheses instead of square brackets:
a = ('spam', 'egg', 'bacon', 'tomato')
Here’s why you would choose to use a tuple instead of list:
- Program execution is faster when manipulating a tuple than it is for the equivalent list.
- You don’t want data modified.
- A Python dictionary requires keys that are of an immutable type.
Here’s an example:
>>> t = ('spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster')
>>> t
('spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster')
>>> type(t)
<class 'tuple'>
>>> t[0]
'spam'
>>> t[-1]
'lobster'
>>> t[3:6]
('tomato', 'ham', 'lobster')
>>> t[1:4]
('egg', 'bacon', 'tomato')
>>> t[0:6:2]
('spam', 'bacon', 'ham')
>>> t[::-1]
('lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam')
>>> t
('spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster')
>>> t[2]
'bacon'
>>> t[2] = 'butter'
Traceback (most recent call last):
File "<input>", line 1, in <module>
t[2] = 'butter'
TypeError: 'tuple' object does not support item assignment
>>> a = 'spam'
>>> b = 42
>>> a, 3.14159, b
('spam', 3.14159, 42)
>>> t = ()
>>> type(t)
<class 'tuple'>
>>> t
()
>>> t = (1, 2)
>>> type(t)
<class 'tuple'>
>>> t = (2)
>>> t
2
>>> type(t)
<class 'int'>
>>> t = (2, )
>>> type(t)
<class 'tuple'>
>>> t
(2,)
00:01 In this video, you’re going to start using tuples. So, a quick note on pronunciation—hey, pick your side. I’ve heard both of these many times: “too-ple” and “tup-ple”.
00:11 The first is “too-ple,” like I’ve been using. That would be like “pupil” or “quadruple”. But there are just as many people I’ve heard pronounce it as “tup-ple,” like “supple.” And what strange about numbering is sometimes you’ll see things go from triple to quadruple to “quin-too-ple,” or some people will say “quin-tup-ple.” You’re going to hear both. Don’t have a big fight about it.
00:32 Pick one and live happily with your choice. So, tuples are identical to lists in all respects except for a couple of these following notes. Tuples are defined by enclosing the elements in parentheses instead of, like lists, using square brackets. It looks something like this.
00:51 The other major thing is that tuples are immutable. So, why would you use a tuple instead of a list? When you’re executing programs with tuples compared to lists, the execution could be faster when you’re manipulating the tuple.
01:06 Now, this is probably not going to be noticeable when you have a small list or a small tuple, but if these are large arrays of information, then you’ll start to see a performance difference.
01:18 There are times when you don’t want your data to be modified. If you have values as part of the collection and they’re meant to remain constant for the life of the program, using a tuple is going to guard against any accidental modification.
01:31 And I know Python dictionaries are not the focus of this course—you’ll learn that the keys in order to create a Python dictionary require that they’re an immutable type. So in that case, you would need to use something immutable like a tuple instead of a list if you’re looking to use a sequence for the key.
01:52 To define a tuple, instead of using square brackets for a list, your objects are going to be contained within parentheses.
02:05
Here’s t
, which is a tuple. And accessing t
by indexing works the same,
02:20 And you can see here it returned a tuple.
02:31 And what if you use the favorite negative stride?
02:36
And you can see it here returning in parentheses these tuples. And just note, yes, you’re still using the same indexing style of square brackets. So, what’s so different? Well, you can’t modify it, so if you were to try to take index 2
and assign something to it, you’re going to raise this exception of a TypeError:
'tuple' object does not support item assignment
.
03:02
If you were to create several objects here, and they can be again of different types, and you were simply to list them out and putting a comma (,
) between each one,
03:17
you’ll see that that comes across as a tuple. And in fact, if you were to start and end the parentheses with nothing in there and you check the type()
on it, what you have there is an empty tuple.
03:29
Now what happens, though, if you try creating a singleton tuple? You say t =
just the number 2
within your parentheses. Well, you can see here t
doesn’t have the parentheses around it, and if you look at its type()
, it’s just an integer.
03:48
Since parentheses are also used to define operator precedence for expressions, Python is going to evaluate the expression of just (2)
in the parentheses as simply the integer 2
and create an int
.
03:59 If you really want to tell it that that’s a singleton, then you actually are going to need to do an extra step, which is to include a trailing comma. That, then, would be considered a tuple.
04:13 It’s probably rare that you’ll need to define a singleton tuple, but if you were to see it in an example, now you’re ready. Next up, you’re going to dive a little deeper into tuple assignment with packing and unpacking.
Become a Member to join the conversation.
Pygator on Sept. 29, 2019
Never thought about singleton tuples, never tried to make such a tuple in all my years of python programming!