CS1101 Discussion Assignment Unit 7 - How tuples can be useful with loops over lists and dictionaries

Questions

Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples. Do not copy them from the textbook or any other source.

Your descriptions and examples should include the following: the zip function, the enumerate function, and the items method.

1. Part1

Before describing how tuples can be useful with loops over lists and dictionaries, we first need to clarify the iterator, the zip function, the enumerate function, and the items method.

The iterator:
When we say that an object is an iterator, it means that we can use loops, such as for loops, to traverse the values in this object.

The zip function:
Parameters: Two or more sequences, like strings, lists or their combinations.
Return value: A zip object.
(1) In the object, “it is a lot of tuples where each tuple contains one element from each sequence” (Downey, 2015, p. 118).
(2) The zip object is a kind of iterator (Downey, 2015, p. 119).

The enumerate function:
Parameters: A sequence, like a string or a list.
Return value: An enumerate object.
(1) In the object, “it is a lot of tuples where each tuple contains one index and its corresponding element from the sequence” ((Downey, 2015, p. 119).
(2) The enumerate object is a kind of iterator.

The items method:
Parameters: items is a method of dictionaries, and it has no parameters.
Return value: A dict_items object.
(1) In the object, it includes a lot of tuples.
(2) The dict_items object is a kind of iterator.

2. Part2

2.1. Description of how tuples can be useful with loops over lists

2.1.1 Description 1 using the zip function

If we have two lists, and we use a zip function to combine them. The return value of the zip function is a zip object. Now, we want to select the element inside the object. Unlike a list, we cannot use an index and square bracket to choose the element (Downey, 2015). Instead, we can use the for loop to traverse the zip object because it is a kind of iterator. As I mentioned above, there are many tuples in the zip object, so we can print the tuple to see what it includes.

Example 1:
Codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
# define two lists
list1 = ['Bob', 'Tom', 'Jade', 'Jene']
list2 = ['boy', 'boy', 'girl', 'girl']

# combine the two lists
res = zip(list1, list2)

# The wrong way to select the element in the zip object
print(res[0]) # WRONG, TypeError: 'zip' object is not subscriptable

# The correct way is to traverse iterator (the the zip object)
for t in res:
print(t)

Results:

1
2
3
4
('Bob', 'boy')
('Tom', 'boy')
('Jade', 'girl')
('Jene', 'girl')

As we can see, when I tried to get the element in the res using res[0], the interpreter returned a TypeError. Conversely, when I used the for loop to traverse the zip iterator, I successfully printed the element in it, and it is a tuple, like (‘Bob’, ‘boy’).

2.1.2 Description 2 using the enumerate function

If we have a list and want to traverse the list to get the elements and their corresponding indexes, we can use the enumerate function (Downey, 2015). When we pass a list as an argument to the parameter of the enumerate function, it will return an enumerate object. Again, unlike a list, we cannot use an index and square bracket to choose the element (Downey, 2015). Instead, we can use the for loop to traverse the object because it is a kind of iterator. As I mentioned above, there are many tuples in the enumerate object, so we can print the tuple to see what it includes. Furthermore, we can also use tuple assignment in the for loop and then print the elements and their indexes.

Example 2:
Codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# define a list
list1 = ['Bob', 'Tom', 'Jade', 'Jene']

# pass a list to the parameter of the enumerate function
res = enumerate(list1)

# The wrong way to select the element in the enumerate object
print(res[0]) # WRONG, TypeError: 'enumerate' object is not subscriptable

# The correct way is to traverse iterator (the the zip object)
for t in res:
print(t)

# Use tuple assignment to get the element in the enumerate object
for index, element in res:
print(index, element)

Results of derectly printing the tuple:

1
2
3
4
(0, 'Bob')
(1, 'Tom')
(2, 'Jade')
(3, 'Jene')

Results of printing the tuple with a tuple assignment:

1
2
3
4
0 Bob
1 Tom
2 Jade
3 Jene

2.2. Description of how tuples can be useful with loops over dictionaries

2.2.1. Description using the items method

We can use the items method if we want to traverse a dictionary to get the key-value pairs. This method will return a dict_items object, and in this object, it is a list that includes tuples. Again, unlike a list, we cannot use an index and square bracket to choose the element (Downey, 2015). Instead, we can use the for loop to traverse the object because it is a kind of iterator. As I mentioned above, there are many tuples in the dict_items object, so we can print the tuple to see what it includes. Furthermore, we can also use tuple assignment in the for loop and then print the key-value pairs.

Example 3:
Codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# define a dictionary
d = {'Bob': 'boy', 'Tom': 'boy', 'Jade': 'girl', 'Jene': 'girl'}

# call the items method
res = d.items()

# The wrong way to select the element in the dict_items object
print(res[0]) # WRONG, TypeError: 'dict_items' object is not subscriptable

# The correct way is to traverse iterator (the dict_items object)
for t in res:
print(t)

# Use tuple assignment to get the element in the dict_items object
for index, element in res:
print(index, element)

Results of derectly printing the tuple:
('Bob', 'boy')
('Tom', 'boy')
('Jade', 'girl')
('Jene', 'girl')

Results of printing the tuple with a tuple assignment:

1
2
3
4
Bob boy
Tom boy
Jade girl
Jene girl

As we can see, when I tried to get the element in the res using res[0], the interpreter returned a TypeError. Conversely, when I used the for loop to traverse the dict_items iterator, I successfully printed the element in it, and it is a tuple, like (‘Bob’, ‘boy’). Furthermore, when I used the for loop with a tuple assignment to traverse the dict_items iterator, the element in the iterator can also be printed, and the results were values like Bob boy.

Word count: 1013

References

1.Downey, A. (2015). Think Python. Green Tea Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf

​


This is the ending, thanks for reading.