Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Function has a list as argument. How to return another list without changing the first?

I'm pretty new in Python (and programming as a whole). I'm pretty sure the answer to this is obvious, but I really don't know what to do.

def do_play(value, slot, board):
    temp=board
    (i,j) = slot
    temp[i][j] = value
    return temp

board is a list of lists. value is an integer. slot is and integer tuple.

What I am trying to do here is to

  • feed the function board
  • copy board to a new list called temp
  • insert a new value in a specific location in temp
  • return temp, leaving board unchanged

When I run this is the shell, both the the original list (board) and the new list (temp) change. = \

Any help would be appreciated.

like image 258
Roughmar Avatar asked Apr 11 '26 22:04

Roughmar


1 Answers

temp=board does not make a new board. It makes the temp variable reference the very same list as board. So changing temp[i][j] changes board[i][j] too.

To make a copy, use

import copy
temp=copy.deepcopy(board)

Note that temp=board[:] makes temp refer to a new list (different than board, but the contents (that is, the lists within the list) are still the same:

In [158]: board=[[1,2],[3,4]]    
In [159]: temp=board[:]    

Modifying temp modifies board too:

In [161]: temp[1][0]=100    
In [162]: temp
Out[162]: [[1, 2], [100, 4]]    
In [163]: board
Out[163]: [[1, 2], [100, 4]]

id shows the object's memory address. This shows temp and board are different lists:

In [172]: id(temp)
Out[172]: 176446508

In [173]: id(board)
Out[173]: 178068780   # The ids don't match

But this shows the second list inside temp is the very same list inside board:

In [174]: id(temp[1])
Out[174]: 178827948

In [175]: id(board[1])
Out[175]: 178827948    # The ids are the same

But if you use copy.deepcopy, then the lists within the list also get copied, which is what you need if modifying temp is to leave board unchanged:

In [164]: import copy    
In [165]: board=[[1,2],[3,4]]    
In [166]: temp=copy.deepcopy(board)    
In [167]: temp[1][0]=100    
In [168]: temp
Out[168]: [[1, 2], [100, 4]]    
In [169]: board
Out[169]: [[1, 2], [3, 4]]
like image 77
unutbu Avatar answered Apr 17 '26 06:04

unutbu



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!