List Manipulation

Steven D'Aprano steve at REMOVETHIScyber.com.au
Tue Jul 4 10:38:02 EDT 2006


On Tue, 04 Jul 2006 07:01:55 -0700, Roman wrote:

> I would appreciate it if somebody could tell me where I went wrong in
> the following snipet:
> 
> When I run I get no result

What do you mean? Does it print None?

> cnt = 0
> p=[]
> reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
>                          quotechar="'", delimiter='\t')
> for line in reader:
>     if cnt > 6:
>        break

That's a very unPythonic way of doing the job. The usual way of doing
this would be something like this:

for line in reader[:7]:
    # no need for the "if cnt > 6: break" clause now


>     for col in line:
>        p[:0].append(str(col))

p[:0] creates a new list, which has a string appended to it, and is then
thrown away. What are you trying to do?

If you are trying to insert the new entry at the beginning of the list,
you probably want this:

p.insert(0, str(col))






More information about the Python-list mailing list