Section 13: Tuples and Enumerate Flashcards

1
Q

Comparison

Tuples vs. Lists

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Comparison

Tuples vs. Sets

A

Useful when we do not need to store duplications, sets are better and faster option

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define

Tuple

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does Tuple indexing work?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Functions

What is the tuple function?

A

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’)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why use tuples instead of lists:

A
  1. Program is faster w tuples
  2. Using tuples ensures values in collection remains constant throughout program
  3. Use tuples as keys in dictionary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define

Packing

A

In string, list, or tuple, packing several elements into one object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define

Packing

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Define

Unpacking

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

for loop

How to use a for loop for tuples:

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the first option for iterating through a tuple using a for loop?

A
  • Iterate over list tuples using for loop
  • Access each element of the tuple using indices
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the second option for iterating through a tuple using a for loop?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do tuples function when inputs for dictionaries?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does the item() function work for a dictionary?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Functions

enumerate function:

A

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 a list, it becomes a list of tuples
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are iterable objects?

A
  • When we can step through the object as a collection of elements
  • Types: str, list, dict, tuple

Use enumerate to keep a counter while iterating through objects

```python
»> s = ‘Night’

> > > x = enumerate(s)
type(x)

<class, ‘enumerate’>

> > > y = list(x)
y
[(0, ‘N’), (1, ‘i’), (2, ‘g’), (3, ‘h’), (4, ‘t’)]
~~~

  • **xand y**: function returns an object of type enumerate.
    If converted into a list, list becomes a tuple
17
Q

Functions

Combining a enumerate() function and a for loop

A
  • Most useful when using a for loop

Lets u access both the element and its index at each iteration

  • index, char: At each iteration, variable char is assigned value of the next character in the string s, while the variable index is assigned the value of the index of char in the string s.At each iteration, next tuple in the enumerate object is unpacked into two variables index and char