[Tutor] Append to list
steve at pearwood.info
Wed May 9 19:46:17 EDT 2018
On Wed, May 09, 2018 at 10:56:45AM -0700, Rick Jaramillo wrote:
> I’m having trouble understanding the following behavior and would greatly appreciate any insight.
> l = [1,2,3,4]
> for i in range(l):
That's not actually your code, is it? Because range(l) gives a
TypeError. In future, please copy and paste your code, don't retype it
> print l
You are appending the same list each time, not a copy. Instead make a
copy by taking a slice from the current position to the end of
for i in range(len(l)):
will do what you want.
More information about the Tutor