-1

I'd like to substitue '(', ')' with '[', ']' in the folliwing list using slicing.

    # the raw data 
    square = ['(', ')', '.', '^', '-']
    # the result I want
    square = ['[', ']', '.', '^', '-']

Replace with slicing method

    In [45]: square[:1] = ['[', ']']
    In [46]: square
    Out[46]: ['[', ']', ']', '.', '^', '-']
  # sliced with wrong index, it should be [:2]

I have to manipulate on the modified square like:

 square.remove(']')

Or re-assign to square manually

    In [47]: square = ['(', ')', '.', '^', '-']
        ...: square[:2] = ['[', ']']
    In [48]: square
    Out[48]: ['[', ']', '.', '^', '-']

During the process, how to reset the modified square to its unmodified status?

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138

1 Answers1

2

I would create a new list from the old one using list()

square = ['(', ')', '.', '^', '-']
backup_square = list(square)
# Here I take the 1st value from square list and I put different data in it
square[0] = "DIFFERENT VALUE"

You will then be able to access the previous version of square in backup_square

print backup_square
>>> ['(', ')', '.', '^', '-']
Pitto
  • 8,229
  • 3
  • 42
  • 51