[Tutor] packing a list of lists

kevin parks kp8 at mac.com
Fri Aug 28 18:53:25 CEST 2009


On Aug 29, 2009, at 12:23 AM, Michael M Mason wrote:

> i wrote:
>> def pack(in_seq):
>> 	out_list=[]
>> 	x = 1
>> 	ll=[1, 1]
>> 	for each in in_seq:
>> 		ll[0] = x
>> 		ll[1] = each
>> 		out_list.append(ll)
>> 		#print ll
>> 		x = x + 1
>> 	print out_list
>
> Variable out_list consists of list ll repeated however many times.  
> Each
> time you change ll you're changing it everywhere it appears in  
> out_list.
> That is, what's being appended to out_list isn't a copy of ll, it's a
> pointer to ll.
>
> You need something like:-
>
>        out_list.append([ll[0],ll[1]])

Right... ugh.. Totally forgot about that. Python 101. I don't know why  
my brain resists
that idea. Every time i let python alone a while and come back to it i  
get bit by this.


>
> And you need to add a return at the end of the function, otherwise it
> returns None:
>
> 	return out_list
>

That, of course, i know... I was messing around debugging of course.

Thanks for the response.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090829/35a7f553/attachment-0001.htm>


More information about the Tutor mailing list