Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Python) adding a list to another without the brackets

Tags:

python

I'm currently making my first project in Python - tic tac toe as i thought it would be a good place to start and I've finally hit a road block.

I've currently got the board setup as a list - Board = [1,2,3...,9] and to reset, Board's contents is deleted and re-entered by a separate list however it also adds the brackets.

Is there a way to grab the contents of a separate list without also grabbing the brackets?

if newGame == 'Y' or 'y':
     del Board[:]
     Board.append(startBoard)
     print Board #for testing
 else:
     sys.exit('game over')

What I'm getting is looking like this:

[[1,2,3,4,5,6,7,8,9]]
like image 401
Chirpas Avatar asked Nov 14 '25 22:11

Chirpas


1 Answers

Use extend.

Board.extend(startBoard)

Alternatively you can do:

Board = [el for el in startBoard]

and omit the del Board[:] althogether.

Alternatively you can use the copy module:

Board = copy.deepcopy(startBoard)

For me the best will be this though:

Board = [i+1 for i in xrange(9)]

Or the simpler:

Board = range(1, 10) # python 2

or

Board = list(range(1, 10)) # python 3

As zehnpaard suggested in the comments.

Also you can do what Erik Allik has proposed in his answer.

like image 124
dmg Avatar answered Nov 17 '25 22:11

dmg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!