2

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?

3 Answers 3

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Could you elaborate with maybe a nested list? I think i am starting to grasp it. Could you maybe go more into detail what you mean by destructuring?
Updated my answer
1

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.

Comments

0

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.

2 Comments

I understand that it gets destructured but into what? count and row get assigned to what part of the list? Or am I understanding this wrong? Could you elaborate with another example with nested lists like in my code?
@itsolidude the tuple gets destructured.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.