How to 'update' or 'overwrite' a python list

pyLearner picture pyLearner · Aug 20, 2014 · Viewed 189.6k times · Source
aList = [123, 'xyz', 'zara', 'abc']
aList.append(2014)
print aList

which produces o/p [123, 'xyz', 'zara', 'abc', 2014]

What should be done to overwrite/update this list. I want the o/p to be

[2014, 'xyz', 'zara', 'abc']

Answer

Rahul Tripathi picture Rahul Tripathi · Aug 20, 2014

You may try this

alist[0] = 2014

but if you are not sure about the position of 123 then you may try like this:

for idx, item in enumerate(alist):
   if 123 in item:
       alist[idx] = 2014