itsolidude
itsolidude

Reputation: 1259

What does adding a second parameter in a for loop with nested enumerate() do?

I'm trying myself on Python again and I want to create a text-based Ticktacktoe.

Currently, I am working on the layout with 0 representing the empty spaces in the games and numbers up top and on the left for coordinates.This is what I have so far:

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],]

print('   0  1  2')

for row in enumerate(game):
    print(row)

which outputs this :

     0  1  2
(0, [0, 0, 0])
(1, [0, 0, 0])
(2, [0, 0, 0])

The problem is that I want it to look like this:

   0  1  2
0 [0, 0, 0]
1 [0, 0, 0]
2 [0, 0, 0]

Now I found a way by adding 'count' into the for loop. But I don't understand why at all. Looking up the Python documentation did not help. Why is this happening? Why does that get rid of the brackets and commas?

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],]

print('   0  1  2')

for count, row in enumerate(game):
    print(count, row)

Edit: I think I understand now.Since enumarate() returns the Index and the value all that's basically happening is that I assigned count to the Index and row to basically the value? And that is why there are now brackets since print prints more than 1 variable using a space? Could you confirm this?

Upvotes: 2

Views: 171

Answers (3)

Michael Bianconi
Michael Bianconi

Reputation: 5242

for row in enumerate(game):

This will print row, which is a tuple. Tuples are printed in the format (item1, item2, ...)

Example:

>>> row = (5, [0,0,0])
>>> print(row)
(5, [0,0,0]) 

for count, row in enumerate(game): Here, the result is unpacked. What was a tuple is now split into count and row. print can take a variable number of arguments, and by default it separates them with a space.

Example:

>>> count = 5
>>> row = [0,0,0]
>>> print(count, row)
5 [0,0,0]

Nested Lists

A nested list is a list that contains other lists. A simple example is

nested = [[0,1,2], [3,4,5]]

You can unpack a nested list (or any list at all)

list012, list345 = nested

That's what for count, row in enumerate(game): is doing.

enumerate(game) returns a tuple: a counter and the normal object. We can compare it like this:

my_list = [10,20,30]
for i in my_list:
    print i    # prints 10 20 30
for i in enumerate(my_list):
    print i    # prints (0,10) (1,20) (2,30)

Upvotes: 3

LLSv2.0
LLSv2.0

Reputation: 543

enumerate(items) returns a list of tuples of a number and your item, where the number increases by one for each new item in the list (counting). In this case enumerate returns the following tuples:

{0, [0, 0, 0]}, {1, [0, 0, 0]}, {2, [0, 0, 0]}

By using a second variable name in the for loop you are destructuring the tuple into count and row. Whereas before you were only giving 1 variable name, so the variable was assigned the entire tuple.

Upvotes: 0

lumbric
lumbric

Reputation: 9093

In the first example a tuple will be passed to print(), which is printed enclosed in parenthesis. In the second answer the row count and the row itself are passed as separate arguments to print() - print() will then print the arguments space separated.

Upvotes: 1

Related Questions