Now that we know about lists, let's look at their sibling: the so-called tuples.
Tuples, just like lists, can contain n elements. A Tuple with two elements is a pair; with three elements it's a 3-tuple and with four elements it's a 4-tuple, etc.
There are tuples with one element and with null elements (empty tuple), but we will not deal with them at the beginning.
Tuples are created as lists, they do not have square brackets around them. Just the commas between the elements are enough.
They behave almost like lists, but they cannot change.
They don't have methods like append
and pop
, and cannot be assigned to elements.
But they can be used in for
loops
and they can read individual elements.
people = 'mom', 'aunt', 'grandmother'
for person in people:
print(person)
print('First is {}'.format(people[0]))
Does this look familiar?
We have already used tuples in
for greeting in 'Ahoj', 'Hello', 'Hola', 'Hei', 'SYN'
If you want to pass a tuple to a function, there will be a problem that a comma separates the individual arguments. In similar cases, you have to encapsulate the tuple into brackets to make it clear that it is one value.
list_of_pairs = []
for i in range(10):
#`append` takes only one argument; we'll give it one pair
list_of_pairs.append ((i, i ** 2))
print(list_of_pairs)
Tuples are useful if you want to return more than one value from the function. You simply declare the return values with a comma between them. It looks like you're returning a few values, but in fact, only one tuple is returned.
def floor_and_remainder(a, b):
return a//b, a%b
Such a floor_and_remainder function already exists
in Python: it's called divmod
and it's always
available (you don't have to import it).
Python can do another trick: if you want to assign values into several variables at once, you can just separate the variables (the left side) by a comma, and the right side can be some "compound" value - for example a tuple.
floor_number, remainder = floor_and_remainder(12, 5)
A tuple is the best for this purpose, but
it works with all the values that can be used with a for
loop:
x, o = 'xo'
one, two, three = [1, 2, 3]
zip
is an interesting function.
It is used in for
loops, just like the range
function that returns numbers.
When zip
gets two lists (or other values that can be used in a for
loop),
it returns pairs -- the first element of the first list is paired with
the first element of the second list,
then the second element with the second, the third element with the third and so on.
It is useful when you have two lists with the same structure - the relevant elements "belong" together and you want to process them together:
people = 'mom', 'aunt', 'grandmother', 'assassin'
properties = 'good', 'nice', 'kind', 'insidious'
for person, property in zip(people, properties):
print ('{} is {}'.format(person, property))
When zip
gets three lists it will return triplets, and so on.
The other function that returns pairs is enumerate
.
As an argument, it takes a list (or other values that can be used in a for
loop)
and it pairs up the element's index (its order in the list) with the respective element.
So the first element will be (0, first element of the given list), then
(1, second element), (2, third element) and so on.
prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
for i, prime_number in enumerate(prime_numbers):
print('Prime number on position {} is {}'.format(i, prime_number))
How to create a tuple with no or one element? Like this:
empty_tuple = ()
one_elem_tuple = ('a',)
The second example works also without brackets - one_el_tuple = 'a',
but it looks like a forgotten comma.
When you really need a single-element tuple,
you should better encapsulate it for clarity.
Lists are used when you do not know in advance
how many values you will have,
or when there are a lot of values.
For example, a list of words in a sentence,
a list of contest participants, a list of moves in a game,
or a list of cards in a deck.
In contrast, in for greeting in 'Ahoj', 'Hello', 'Hola', 'Hei', 'SYN'
we are using a tuple.
Tuples are often used for values
of different types where each "position"
inside the tuple has a different meaning.
For example, you can use a list for the letters of the alphabet,
but for pairs of index-value from enumerate
, you'd use a tuple.
The empty tuple and one-element tuple are a little strange, but they exist: For example, the list of playing cards in your hand, or the list of people currently enrolled in the competition may occasionally be empty.
Lists and tuples also have technical limits: Tuples cannot be changed, and when we will learn how to work with dictionaries, we will find that lists cannot be used as dictionary keys.
Often, it is not entirely obvious which type to use -- in that case, it probably doesn't really matter. Follow your instinct. :)
{ "data": { "sessionMaterial": { "id": "session-material:2018/pyladies-en-prague:list:1", "title": "Tuples", "html": "\n \n \n\n <h1>Tuples</h1>\n<p>Now that we know about lists, let's look at their sibling: the so-called\n<em>tuples</em>.</p>\n<p>Tuples, just like lists, can contain <var>n</var> elements.\nA Tuple with two elements is a <em>pair</em>; with three\nelements it's a <em>3-tuple</em> and with four elements\nit's a <em>4-tuple</em>, etc.</p>\n<div class=\"admonition note\"><p>There are tuples with one element\nand with null elements (<em>empty tuple</em>),\nbut we will not deal with them at the beginning.</p>\n</div><p>Tuples are created as lists, they do not have square brackets around them.\nJust the commas between the elements are enough.</p>\n<p>They behave almost like lists, but they cannot change.\nThey don't have methods like <code>append</code>\nand <code>pop</code>, and cannot be assigned to elements.\nBut they can be used in <code>for</code> loops\nand they can read individual elements.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">people</span> <span class=\"o\">=</span> <span class=\"s1\">'mom'</span><span class=\"p\">,</span> <span class=\"s1\">'aunt'</span><span class=\"p\">,</span> <span class=\"s1\">'grandmother'</span>\n<span class=\"k\">for</span> <span class=\"n\">person</span> <span class=\"ow\">in</span> <span class=\"n\">people</span><span class=\"p\">:</span>\n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">person</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'First is {}'</span><span class=\"o\">.</span><span class=\"n\">format</span><span class=\"p\">(</span><span class=\"n\">people</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]))</span>\n</pre></div><div class=\"admonition note\"><p>Does this look familiar?\nWe have already used tuples in\n<code>for greeting in 'Ahoj', 'Hello', 'Hola', 'Hei', 'SYN'</code></p>\n</div><p>If you want to pass a tuple to a function, there will be a problem \nthat a comma separates the individual arguments.\nIn similar cases, you have to encapsulate the tuple into\nbrackets to make it clear that it is one value.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">list_of_pairs</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n<span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"mi\">10</span><span class=\"p\">):</span>\n <span class=\"c1\">#`append` takes only one argument; we'll give it one pair</span>\n <span class=\"n\">list_of_pairs</span><span class=\"o\">.</span><span class=\"n\">append</span> <span class=\"p\">((</span><span class=\"n\">i</span><span class=\"p\">,</span> <span class=\"n\">i</span> <span class=\"o\">**</span> <span class=\"mi\">2</span><span class=\"p\">))</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">list_of_pairs</span><span class=\"p\">)</span>\n</pre></div><p>Tuples are useful if you want to return \nmore than one value from the function.\nYou simply declare the return values with a comma between them.\nIt looks like you're returning a few values, but\nin fact, only one tuple is returned.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">def</span> <span class=\"nf\">floor_and_remainder</span><span class=\"p\">(</span><span class=\"n\">a</span><span class=\"p\">,</span> <span class=\"n\">b</span><span class=\"p\">):</span>\n <span class=\"k\">return</span> <span class=\"n\">a</span><span class=\"o\">//</span><span class=\"n\">b</span><span class=\"p\">,</span> <span class=\"n\">a</span><span class=\"o\">%</span><span class=\"n\">b</span>\n</pre></div><div class=\"admonition note\"><p>Such a floor_and_remainder function already exists \nin Python: it's called <code>divmod</code> and it's always \navailable (you don't have to import it).</p>\n</div><p>Python can do another trick: if you want to assign values\ninto several variables at once, you can just separate the variables \n(the left side) by a comma, and the right side can be some \n"compound" value - for example a tuple.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">floor_number</span><span class=\"p\">,</span> <span class=\"n\">remainder</span> <span class=\"o\">=</span> <span class=\"n\">floor_and_remainder</span><span class=\"p\">(</span><span class=\"mi\">12</span><span class=\"p\">,</span> <span class=\"mi\">5</span><span class=\"p\">)</span>\n</pre></div><p>A tuple is the best for this purpose, but\nit works with all the values ​​that can be used with a <code>for</code> loop:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">o</span> <span class=\"o\">=</span> <span class=\"s1\">'xo'</span>\n<span class=\"n\">one</span><span class=\"p\">,</span> <span class=\"n\">two</span><span class=\"p\">,</span> <span class=\"n\">three</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">]</span>\n</pre></div><h2>Functions returning tuples</h2>\n<p><code>zip</code> is an interesting function.\nIt is used in <code>for</code> loops, just like the <code>range</code> function that returns numbers.</p>\n<p>When <code>zip</code> gets two lists (or other values that can be used in a <code>for</code> loop),\nit returns pairs -- the first element of the first list is paired with\nthe first element of the second list,\nthen the second element with the second, the third element with the third and so on.</p>\n<p>It is useful when you have two lists with the same\nstructure - the relevant elements "belong" together\nand you want to process them together:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">people</span> <span class=\"o\">=</span> <span class=\"s1\">'mom'</span><span class=\"p\">,</span> <span class=\"s1\">'aunt'</span><span class=\"p\">,</span> <span class=\"s1\">'grandmother'</span><span class=\"p\">,</span> <span class=\"s1\">'assassin'</span>\n<span class=\"n\">properties</span> <span class=\"o\">=</span> <span class=\"s1\">'good'</span><span class=\"p\">,</span> <span class=\"s1\">'nice'</span><span class=\"p\">,</span> <span class=\"s1\">'kind'</span><span class=\"p\">,</span> <span class=\"s1\">'insidious'</span>\n<span class=\"k\">for</span> <span class=\"n\">person</span><span class=\"p\">,</span> <span class=\"nb\">property</span> <span class=\"ow\">in</span> <span class=\"nb\">zip</span><span class=\"p\">(</span><span class=\"n\">people</span><span class=\"p\">,</span> <span class=\"n\">properties</span><span class=\"p\">):</span>\n <span class=\"k\">print</span> <span class=\"p\">(</span><span class=\"s1\">'{} is {}'</span><span class=\"o\">.</span><span class=\"n\">format</span><span class=\"p\">(</span><span class=\"n\">person</span><span class=\"p\">,</span> <span class=\"nb\">property</span><span class=\"p\">))</span>\n</pre></div><p>When <code>zip</code> gets three lists it will return triplets, and so on.</p>\n<p>The other function that returns pairs is <code>enumerate</code>.\nAs an argument, it takes a list (or other values that can be used in a <code>for</code> loop)\nand it pairs up the element's index (its order in the list) with the respective element.\nSo the first element will be (0, <em>first element of the given list</em>), then\n(1, <em>second element</em>), (2, <em>third element</em>) and so on.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">prime_numbers</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"mi\">5</span><span class=\"p\">,</span> <span class=\"mi\">7</span><span class=\"p\">,</span> <span class=\"mi\">11</span><span class=\"p\">,</span> <span class=\"mi\">13</span><span class=\"p\">,</span> <span class=\"mi\">17</span><span class=\"p\">,</span> <span class=\"mi\">19</span><span class=\"p\">,</span> <span class=\"mi\">23</span><span class=\"p\">,</span> <span class=\"mi\">29</span><span class=\"p\">]</span>\n\n<span class=\"k\">for</span> <span class=\"n\">i</span><span class=\"p\">,</span> <span class=\"n\">prime_number</span> <span class=\"ow\">in</span> <span class=\"nb\">enumerate</span><span class=\"p\">(</span><span class=\"n\">prime_numbers</span><span class=\"p\">):</span>\n <span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'Prime number on position {} is {}'</span><span class=\"o\">.</span><span class=\"n\">format</span><span class=\"p\">(</span><span class=\"n\">i</span><span class=\"p\">,</span> <span class=\"n\">prime_number</span><span class=\"p\">))</span>\n</pre></div><h2>Small tuples</h2>\n<p>How to create a tuple with no or one element? Like this:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">empty_tuple</span> <span class=\"o\">=</span> <span class=\"p\">()</span>\n<span class=\"n\">one_elem_tuple</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"s1\">'a'</span><span class=\"p\">,)</span>\n</pre></div><p>The second example works also without brackets - <code>one_el_tuple = 'a',</code>\nbut it looks like a forgotten comma.\nWhen you <em>really</em> need a single-element tuple, \nyou should better encapsulate it for clarity.</p>\n<h2>When to use the list and when the tuple?</h2>\n<p>Lists are used when you do not know in advance\nhow many values you will have,\nor when there are a lot of values.\nFor example, a list of words in a sentence,\na list of contest participants, a list of moves in a game,\nor a list of cards in a deck.\nIn contrast, in <code>for greeting in 'Ahoj', 'Hello', 'Hola', 'Hei', 'SYN'</code>\nwe are using a tuple.</p>\n<p>Tuples are often used for values\nof different types where each "position"\ninside the tuple has a different meaning.\nFor example, you can use a list for the letters of the alphabet,\nbut for pairs of index-value from <code>enumerate</code>, you'd use a tuple.</p>\n<p>The empty tuple and one-element tuple are a little strange, but they exist:\nFor example, the list of playing cards in your hand, or the\nlist of people currently enrolled in the competition\nmay occasionally be empty.</p>\n<p>Lists and tuples also have technical limits:\nTuples cannot be changed, and when we will learn how to work with dictionaries,\nwe will find that lists cannot be used as dictionary keys.</p>\n<p>Often, it is not entirely obvious which type to use\n-- in that case, it probably doesn't really matter.\nFollow your instinct. :)</p>\n\n\n " } } }