0% found this document useful (0 votes)
5 views

2D-Lists__intro

The document discusses the use of two-dimensional lists in Python, particularly for representing game boards like tic-tac-toe and connect4. It highlights the advantages of using 2D lists over multiple variables or single-dimensional lists for organizing data and simplifying operations. The document also provides examples of creating and printing these lists using list comprehension and the pprint module.

Uploaded by

itsmuhammadk2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2D-Lists__intro

The document discusses the use of two-dimensional lists in Python, particularly for representing game boards like tic-tac-toe and connect4. It highlights the advantages of using 2D lists over multiple variables or single-dimensional lists for organizing data and simplifying operations. The document also provides examples of creating and printing these lists using list comprehension and the pprint module.

Uploaded by

itsmuhammadk2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Two-Dimensional Lists

As you know the elements of your list can be anything, even lists and tuples
e.g. student = ["Jake", 16, ["Math", "Comp.Sci", "Physics"]]

This is valid and some people will do this in Python. I don't like it much. If I want to create an object that
represents a person I'll make a person class and make an instance of it. A more complicated list that I do like
is a list of lists. e.g.

>>>ticBoard = [["X", "", "X"],["", "O", ""],["O", "", ""]]

This looks messy, I know, lets look at it closer.


>>> ticBoard[0]
['X', '', 'X']
>>> ticBoard[1][1]
'O'

How this board could be drawn on the screen is just a personal preference, as long as I am consistent, it will
work out. I like to be consistent with the way things are already drawn on the screen.

You'll notice this gets a lot easier if I declare my 2-dim list


like this:

ticBoard = [["X", " ", "X"],


[" ", "O", " "],
["O", " ", " "]]

I could store the values in a number of ways. I could use:

9 different variables: tic00 = "X" tic01 = "O” …


A single dimensional list
ticBoard = ["X", " ", "X", " ", "O", " ", "O", " ", " "]

The advantage of using a 2-dimensional list is that I can easily separate the issue of finding the row/column
locations from doing something with it. It also can make checking rows and columns easier. This example
is fairly small, so you might be tempted to use 9 variables. What if we were playing connect4 instead; that's
a 7x6 board, or 42 variables.

You can use list comprehension to create a 3x3 grid for a tic-tac-toe game:
e.g.
board=[[0 for i in range(3)] for j in range(3)]
Try the following program (Creating connect4 board)

from pprint import pprint #pprint is used to print 2D lists in rows and columns

connect=[[0 for i in range(7)] for j in range(6)]

pprint(connect)

Here is the output:

[[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy