List Manipulation

Laszlo Nagy gandalf at designaproduct.biz
Tue Jul 4 10:22:18 EDT 2006


Roman írta:
> I would appreciate it if somebody could tell me where I went wrong in
> the following snipet:
>
> When I run I get no result
>
> cnt = 0
> p=[]
> reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
>                          quotechar="'", delimiter='\t')
> for line in reader:
>     if cnt > 6:
>        break
>     for col in line:
>        p[:0].append(str(col))
>   
You are appending to a slice. In that case, p[:0] creates a new list 
object. You are not appending to p but to a new object (created from a 
slice).
If you need to insert an item at the begining of a list, use the insert 
method instead.

 >>> l = [2,3,4]
 >>> l.insert(1,0)
 >>> l
[2, 0, 3, 4]


Best,

   Laszlo




More information about the Python-list mailing list