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. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor