[Tutor] packing a list of lists

kevin parks kp8 at me.com
Fri Aug 28 16:05:44 CEST 2009


Back to python after a long long layoff. So i am running into some  
beginner's confusion...

I am trying to plot a list of numbers in gnuplot.py. To do that I am  
trying to pack the list with an index by iterating over the list so i  
can get something like:

foo = [12, 11, 9, 6, 2, 9, 3, 8, 12, 3, 5, 6]

[ [1, 12], [2, 11], [3, 9], [4, 6], [5, 2], [6, 9], [7, 3], [8, 8] ... ]

So that i have x, y pairs to plot. When i print in my func i get the  
right thing, for each item (note scaffolding) yet when i reurn the  
whole list i just get the last pair repeated over and over.

I am not sure why this is.


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
		

# function declarations would go here
def test():
	"""test function - say what this does here and skip a line
	
	Keyword arguments:
	none
	"""

	print
	foo = minus(200)
	plot_me = pack(foo)
	#print foo
	print
	print plot_me
	

if __name__ == "__main__":
	test()




More information about the Tutor mailing list