-2
Grid = ["A1","A2","A3","B1","B2","B3","C1","C2","C3"]
  print("This is the grid.")
  for i in range(3):
    print("\t".join(Grid[(i*3):(i*3+3)]))

I was looking for a way to create a 2D 3x3 grid in Python and found this. I want to use it but I'm not sure how line 4 actually works, other than "\t".

Help much appreciated, thanks :)

Leyla
  • 3
  • 2

5 Answers5

1

String.join method combines a collection with a separator defined. In your example, \t is a tab. So you will have a tab between the items in the new string.

>>> Grid = ["A1","A2","A3","B1","B2","B3","C1","C2","C3"]
>>> for i in range(3):
    print("\t".join(Grid[(i*3):(i*3+3)]))


A1  A2  A3
B1  B2  B3
C1  C2  C3

>>> for i in range(3):
    print(" --- ".join(Grid[(i*3):(i*3+3)]))


A1 --- A2 --- A3
B1 --- B2 --- B3
C1 --- C2 --- C3

There is a short tutorial String.join here.

Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • Downvote?! It makes me doubt about democracy. This is the most accurate and comprehensible answer by now. – cezar Aug 30 '17 at 07:14
  • @cezar there will always be haters.. I wonder also why it got downvoted. thanks for your time commenting this – Chen A. Aug 30 '17 at 08:10
  • Maybe one could explain the evaluation of `(i*3):(i*3+3)`, which slices the list in every iteration step: [0:3], [3:6], [6:9]. From the question however it seems the OP doesn't understand the `join` method, so this is a proper answer. – cezar Aug 30 '17 at 08:55
1

lets break it apart:

Grid[(i*3):(i*3+3)]

this is using list slicing. Let's make it easier and say i is 0. then this is

Grid[0:3]

which is the elements of index 0 to 2, so the first three elements of the list. If i is one, its

Grid[3:6]

elements with index 3 to 5, the center 3 elements.

"\t" is the tab character, the join() function puts the preceding string between every element of the list in the parameter. So

print("\t".join(Grid[(i*3):(i*3+3)]))

prints three consecutive elements of the list separated by tabs, then starts a new line.

including the for-loop, the program prints three lines of 3 elements arranged in a 3x3 grid, with "A1" in the top left and "C3" in the bottom right:

A1      A2      A3
B1      B2      B3
C1      C2      C3
Zinki
  • 446
  • 4
  • 10
0

Grid is a list. Square brackets are used to access elements of the list. If there is a colon : in the square brackets this is called slicing and it is used to access several elements. e.g. x[1:4] picks the second to forth element in the list.

Joe
  • 6,758
  • 2
  • 26
  • 47
0

OK, let's break the for loop down:

for i in range(3):
    print("\t".join(Grid[(i*3):(i*3+3)]))

i here just serves the purpose of iterating over the aforementioned list. Within the print() statement in the for loop, the join() method concatenates the elements Grid[(i*3):(i*3+3)] with a tab. Since i changes its value from 0 to 3 over the iterations of the loop, the referenced slice from the list would be Grid[0:3], Grid[3:6], Grid[6:9] respectively. Then it prints the values in each slice, separated by a tab, and a newline afterwards due to the default end='\n' from the print() statement.

Hope this helped!

Jerrybibo
  • 1,315
  • 1
  • 21
  • 27
0

You have nine elements in the Grid list. The for loop iterates three times and each time it selects three consecutive elements from the Grid and prints them spaced with tabs. In the last line the Grid[(i*3):(i*3+3)] part selects three elements. We can select elements from a list using list slicing. Multiple elements in a list can be selected using list_name[0:3]. This will selecting the first three elements. Note that the element with index 3 will not be selected. The result of this slicing is a list.

All the elements are then converted to a single string using the join function. The join takes in a list or tuple of words and a separator that is so to say planted between consecutive elements of the list.

Clock Slave
  • 7,627
  • 15
  • 68
  • 109