[Tutor] Can you modify every nth item in a list with a single assignment?

Hsi-Chen Lee hsi667@hotmail.com
Thu Jun 12 19:00:03 2003


I think one thing you might be able to do is use list comprehensions (after 
v2.0, correct me if I'm wrong).

list2 = [do_something_to_x for x in list1]

It probably satisfies half of your criteria, it's a single statement but 
it's still a loop, plus your technically creating a new list, not modifying 
the old one.  do_something_to_x should be a single statement or you'll 
probably have to wrap it in a lambda function, which would drive me nuts.  
Enjoy.
-Hsi


>From: "R. Alan Monroe" <amonroe@columbus.rr.com>
>Reply-To: "R. Alan Monroe" <amonroe@columbus.rr.com>
>To: tutor@python.org
>Subject: [Tutor] Can you modify every nth item in a list with a single 
>assignment?
>Date: Wed, 11 Jun 2003 22:20:20 -0400
>
>I know I can modify every nth list item using a for loop over a
>range(start,end,n), but can it be done in a single assignment, without
>a loop?
>
>Alan
>
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor

_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*  
http://join.msn.com/?page=features/junkmail