Can a list variable contain items which are also lists of data?
Answer
In Python, a list can contain other lists in addition to more common data types such as strings, integers, and floating point values. Having a list which contains lists is useful when trying to represent a two-dimensional array. A good example of this type of structure is a game board. When used in this scenario, the main list contains other lists which each represent one row of the board. The items in the inside lists represent the columns of the board.
In the example below, a list is used to represent a game board for Tic-Tac-Toe. The associated code will print the board. In this example, the print() function for Python 3 is used which allows for the option end that prints without starting a new line.
tictactoe = [ ['X', 'O', 'X'],
[ 'O', ' ', 'X'],
['X','O','O']]
for row in tictactoe:
for column in row:
print(column + " ", end='')
print()
By default, the print function issues a newline character ('\n') following its output, so subsequent calls to print begin their output on a new line. By supplying an explicit str (string) value for the end parameter, you can alter this behavior for an individual call to print. In the case you cited, an empty string ('') is assigned to end, so the next call to print continues its output on the same line.
So I wanted them to print out as a square, so I did this:
tictactoe = [
[“X”, “O”, “X”],
[“O”, " “, “X”],
[“X”, “O”, “O”]
]
for row in tictactoe:
print()
for column in row:
print(column + " “, end=””)
But it gave me an empty line at beginning when it printed out. I know it’s because I put the print() right after for row in tictactoe, but how can I get my square without the empty line?
If we were to keep almost all of your code how it is right now, there’s one very simple way to accomplish what you are trying to do. Right now, your print() statement prints a blank line for each row before any of the contents in row are printed. You want to print the blank line after the contents of row are printed. Do you see the obvious way to accomplish this?
Here's a hint... but only open it once you've tried to figure it out yourself again.
for a in lst:
for b in a:
# do something
# do something for every a in lst after the nested for loop exits each time
“end” is an additional symbol added after text printed. By default it is ‘\n’ print doc
In this example print(column + " ", end='') every print call wouldn’t add line break
Hi, I"m new to Python and programming in general, but is that how MS Excel or Google Sheets work? Like, a spreadsheet would be a list, a row would be a list within that list, and a column would be index number of a position within that list?
Essentially, yes. A spreadsheet is like a two dimensional list, though much more complex given that both rows and columns can be named.
A one-dimension list consists of one row of data, ordered with index zero to N. A two dimension list will consist of multiple rows of data, with the outer index being the row numbers, and the inner index being the columns.