List Manipulation

Roman rgelfand2 at hotmail.com
Tue Jul 4 11:24:40 EDT 2006


Nothing got printed.

Could you tell me what would be pythonic version of what I am trying to
do?


Diez B. Roggisch wrote:
> > p[j] does not give you a reference to an element inside p.  It gives
> > you a new sublist containing one element from p.  You then append a
> > column to that sublist.  Then, since you do nothing more with that
> > sublist, YOU THROW IT AWAY.
>
> Not correct.
>
> p = [[]]
> p[0].append(1)
> print p
>
> yields
>
> [[1]]
>
> p[0] _gives_ you a reference to an object. If it is mutable (list are) and
> append mutates it (it does), the code is perfectly alright.
>
> I don't know what is "not working" for the OP, but actually his code works
> if one replaces the csv-reading with a generated list:
>
> cnt = 0
> p=[[], [], [], [], [], [], [], [], [], [], []]
> reader = [["column_%i" % c for c in xrange(5)] for l in xrange(7)]
> for line in reader:
>     if cnt > 6:
>        break
>     j = 0
>     for col in line:
>        p[j].append(col)
>        j=j+1
>     cnt = cnt + 1
> print p
>
>
> You are right of course that it is the most unpythonic way imaginabe to do
> it. But it works.
> 
> Diez




More information about the Python-list mailing list