Dear All, I would like to thanks for your responds.
Ricardo Aráoz wrote: Kent Johnson wrote: > Dave Kuhlman wrote: >> Consider the following: >> >> >>> array = [1,2,3,4,5] >> >>> array2 = array >> >>> array = [i * 2 for i in array] >> >>> array >> [2, 4, 6, 8, 10] >> >>> array2 >> [1, 2, 3, 4, 5] >> >> So, did you want array2 to change, or not? >> >> Here is a solution that changes the object that both array and >> array2 refer to: >> >> >>> array = [1, 2, 3, 4, 5] >> >>> array2 = array >> >>> for idx, item in enumerate(array): >> array[idx] = item * 2 >> >>> array >> [2, 4, 6, 8, 10] >> >>> array2 >> [2, 4, 6, 8, 10] >> >> Basically, this modifies the list "in place", rather than making a >> new list from the old one. > > Another way to do this is to assign to a slice of array: > > array[:] = [ item*2 for item in array ] > > Kent Thanks, hadn't really thought about having a copy of array (guess I'm too much of a n00b). Really simple and elegant way of solving it though. I have played the way Kent showed. >>> array = [1, 2, 3, 4, 5] >>> array[2] = [item+5 for item in array] >>> array [1, 2, [6, 7, 8, 9, 10], 4, 5] >>> array = [1, 2, 3, 4, 5] >>> array[2:2] = [item+5 for item in array] >>> array [1, 2, 6, 7, 8, 9, 10, 3, 4, 5] Even more interesting >>> array = [1, 2, 3, 4, 5] >>> array[3:2] = ['new','members', 'in', 'the', 'list'] >>> array [1, 2, 3, 'new', 'members', 'in', 'the', 'list', 4, 5] Yours sincerely, Janos Juhasz _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor