[Tutor] most efficient way to do this

bob gailer bgailer at alum.rpi.edu
Fri Feb 15 03:49:06 CET 2008


Christopher Spears wrote:
> I created a file called arrays.py:
>
> #!/usr/bin/python
>
> locations = ["/home/",
> "/office/" ,
> "/basement/" ,
> "/attic/"]
>
> Now I want to add the word "chris" on to each element
> of the locations list, so I wrote another script
> called chris_arrays.py:
>
> #!/usr/bin/python/
>
> from arrays import locations
>
> add_on = "chris"
> new_loc = []
>
> for i in range(len(locations)):
>     new_loc.append(locations[i] + add_on)
>   
Alan gave the "best" answer.

FWIW I offer an intermediate improvement:

for location in locations:
    new_loc.append(location + add_on)


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC



More information about the Tutor mailing list