Convert tuple to list and back Convert tuple to list and back python python

Convert tuple to list and back


Convert tuple to list:

>>> t = ('my', 'name', 'is', 'mr', 'tuple')>>> t('my', 'name', 'is', 'mr', 'tuple')>>> list(t)['my', 'name', 'is', 'mr', 'tuple']

Convert list to tuple:

>>> l = ['my', 'name', 'is', 'mr', 'list']>>> l['my', 'name', 'is', 'mr', 'list']>>> tuple(l)('my', 'name', 'is', 'mr', 'list')


You have a tuple of tuples.
To convert every tuple to a list:

[list(i) for i in level] # list of lists

--- OR ---

map(list, level)

And after you are done editing, just convert them back:

tuple(tuple(i) for i in edited) # tuple of tuples

--- OR --- (Thanks @jamylak)

tuple(itertools.imap(tuple, edited))

You can also use a numpy array:

>>> a = numpy.array(level1)>>> aarray([[1, 1, 1, 1, 1, 1],       [1, 0, 0, 0, 0, 1],       [1, 0, 0, 0, 0, 1],       [1, 0, 0, 0, 0, 1],       [1, 0, 0, 0, 0, 1],       [1, 1, 1, 1, 1, 1]])

For manipulating:

if clicked[0] == 1:    x = (mousey + cameraY) // 60 # For readability    y = (mousex + cameraX) // 60 # For readability    a[x][y] = 1


You can have a list of lists. Convert your tuple of tuples to a list of lists using:

level1 = [list(row) for row in level1]

or

level1 = map(list, level1)

and modify them accordingly.

But a numpy array is cooler.


matomo