So I was just watching a tutorial about Tic Tac Toe since I wanted to know if you could make the grid with a for loop. And there i saw this line. I understand how a for loop works, but how can I place a " " before a for loop and how does it work? Also I am kinda unsure about the second for loop. What be nice if anyone could explain that to me. Thanks in advance.
sefboard = [" " for i in range(9)]
print(sefboard)
for row in [sefboard[i*3: (i 1)*3] for i in range(3)]:
print("| " " | " " |")
CodePudding user response:
First let's understand comprehensions:
A list, dict, set, etc. can be made with a comprehension. Basically a comprehension is taking a generator and using it to form a new variable. A generator is just an object that returns a different value each iteration so to use list as an example: to make a list with a list comprehension we take the values that the generator outputs and put them into their own spot in a list. Take this generator for example:
x for x in range(0, 10)
This will just give 0 on the first iteration, then 1, then 2, etc. so to make this a list we would use []
(list brakets) like so:
[x for x in range(0, 10)]
This would give:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #note: range does not include the second input
for a dictionary and for a set we use {}
, but since dictionaries uses key-value pairs our generator will be different for sets and dictionaries. For a set it is the same as a list:
{x for x in range(0, 10)} #gives the set --> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
but for a dictionary we need a key and a value. Since enumerate
gives two items this could be useful for dictionaries in some cases:
{key: value for key, value in enumerate([1,2,3])}
In this case the keys are the indexes and the values are the items in the list. So this gives:
{0: 1, 1: 2, 2: 3} #dictionary
It doesn't make a set because we denote x : y
which is the format for items in a dictionary, not a set.
Now we can break this down:
This:
" " for i in range(9)
Is just a generator that returns 9 spaces. Since it is contained in list brackets ([]
) it makes a list of 9 spaces.
This:
[sefboard[i*3: (i 1)*3] for i in range(3)]
Is just getting 3 of the 9 spaces at a time with sefboard[i*3: (i 1)*3]
and making it one of 3 rows via for i in range(3)
(generator contained in list brakets ([]
)) so this gives a 3x3 grid of spaces.
So the for
loop:
for row in [sefboard[i*3: (i 1)*3] for i in range(3)]:
Is just getting each row from the 3x3 grid so it can print on each row:
print("| " " | " " |")
Why is this used?
This code's output is equivalent to:
for _ in range(3):
print("| " " | " " |")
But the sefboard
could be useful for updating the board and printing something other than the blank board. There is probably going to be more lines within the for
loop which will use sefboard
and not just print("| " " | " " |")
. I assume the tutorial you mention will explain why they are doing it like they are rather than using the simpler version above, or it may make sense in the context of the project once you watch the rest of the video.