Section 13: Tuples and Enumerate Flashcards
Comparison
Tuples vs. Lists
- Tuples useful when we dont want to change data and lists when want to modify
- Tuples are faster than Lists.
- Tuples useful if we have constant set of values or need to store different element to be used as dictionary keys
Comparison
Tuples vs. Sets
Useful when we do not need to store duplications, sets are better and faster option
Define
Tuple
Tuple is a ordered collection of objects, like lists
- Tuple is a ordered collection of objects, like lists
- Immutable: But number of elements in the typle, values, cannot change after tuple is created
Can’t add, remove elements either
- Parenthesis optional
python >>> c = (1, 1, 2, 3, 5, 8) # or c = 1, 1, 2, 3, 5, 8 >>> c[4] = 7 **TypeError**: 'tuple' object does not support item assignment
- Often define using
()
rather than[]
time_tuple = (14, 34)
How does Tuple indexing work?
Access specific elements inside tuple using indices, (string or list)
```python
»> tup = (45, 23, ‘abc’)
»> print(tup[1])
23
~~~
- Also do negative indexing and slicing
- Invalid index: IndexError
Functions
What is the tuple
function?
Convert strings and other data types into tuples using tuple()
function
Other functions:len()
, min()
, max()
, sum()
```python
»> t = tuple(‘lupins’)
»> print(t)
(‘l’, ‘u’, ‘p’, ‘i’, ‘n’, ‘s’)
~~~
Why use tuples instead of lists:
- Program is faster w tuples
- Using tuples ensures values in collection remains constant throughout program
- Use tuples as keys in dictionary
Define
Packing
In string, list, or tuple, packing several elements into one object
Define
Packing
In string, list, or tuple, packing several elements into one object
Unpacking: Assign values in a string/lst/tuple to multiple variables (if the exact length is known)
Define
Unpacking
Unpacking: Assign values in a string/lst/tuple to multiple variables (if the exact length is known)
```python
s = “odin”
a, b, c, d = s
print(a) #o
print(b) #d
print(c) #i
print(d) #n
~~~
```python
new_tuple = (‘Valhalla’, 7)
z, y = new_tuple
print(new_tuple[0]) #’Valhalla’
print(z) #’Valhalla’
~~~
-
ValueError
: if the num of variables on the left is not equal to the num of values in the object on the right
Since the len of the tuple does not change, it is easier to use packing with than lists
for
loop
How to use a for
loop for tuples:
for
loop:
```python
other_tuple = 1, 3, 6
for t in other_tuple:
print(t)
~~~
for
loop and a list of tuples:
- Iterate over list tuples using for loop
- Access each element of the tuple using indices
What is the first option for iterating through a tuple using a for
loop?
- Iterate over list tuples using for loop
- Access each element of the tuple using indices
What is the second option for iterating through a tuple using a for
loop?
Option #2, special syntax for iterating over at list of tuples (if all the tuples in the list have the same size):
Each element of the list is a tuple, at each iteration of the loop, the tuple is unpacked into two variables
How do tuples function when inputs for dictionaries?
Pass as input to dict as list of tuples of length two to create dictionary:
items()
:
- Use method
items()
to retrieve set-like object containing items from dict as tuples - If you cant to copy → cast to a list
How does the item()
function work for a dictionary?
items()
:
- Use method
items()
to retrieve set-like object containing items from dict as tuples - If you cant to copy → cast to a list
Functions
enumerate
function:
enumerate
built-in function in Python
- Loop over something iterable and have an automatic counter
- Function returns an object of type
enumerate
.
If converted into alist
, it becomes a list oftuples