Can a list variable contain another list?

Question

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()
14 Likes

What does, end=’ ’ , does here?

7 Likes

Hello, and welcome @pyrockstar05796,

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.

See Built-in Functions: print ( *objects , sep=’ ’ , end=’\n’ , file=sys.stdout , flush=False ).

19 Likes

Thank you So much @appylpye :slight_smile:

1 Like

Sorry but i didn’t get it. Maybe the fact that i haven’t yet learned the for loop is to blame.
Could you please explain how this code works?

3 Likes

I assume your question refers to the “list of lists”, and not end = “”.

The for - in loop is key here. for - in sees every list as [thing_1, thing_2, thing_3, … ]. It does not look more deeply into thing_1, etc.

If the list elements have any deeper structure (for example, are themselves lists), an inner for - in loop is required to explore them.

3 Likes

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?

1 Like

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

Welcome to the forums!

3 Likes

Thank you so much doctor!

Happy to be of help! I’m not a doctor :laughing:, though I see how my username is potentially misleading…

1 Like

“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

It stops going to the next line

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.

1 Like

Maybe concatenation with the new list will work.