Skip to content Skip to sidebar Skip to footer

Convert Lists Into 'transposed' List

This is probably a beginners question but I don't know how to search for an answer (because I cannot 'name' the question) I have 2 lists or a tuple of 2 lists xxx = ['time1', 'tim

Solution 1:

Use the builtin zip function:

zzz = zip(xxx, yyy) 

Of course, this creates a list of tuples (or an iterable of tuples in python3.x). If you really want a list of lists:

#list (python2.x) or iterable(python3.x) of listszzz = map(list,zip(xxx,yyy)) 

or

#list of lists, not list of tuples#python 2.x and python 3.xzzz = [ list(x) for x in zip(xxx,yyy) ]

If you're using python3.x and you want to make sure that zzz is a list, the list comprehsion solution will work, or you can construct a list from the iterable that zip produces:

#list of tuples in python3.x.  zzz = list(zip(xxx,yyy)) #equivalent to zip(xxx,yyy) in python2.x#will work in python2.x, but will make an extra copy.# which will be available for garbage collection# immediately

Solution 2:

I notice that your data includes time stamps and numbers. If you're doing number-intensive calculations, then numpy arrays might be worth a look. They offer better performance, and transposing is very simple. ( arrayname.transpose() or even arrayname.T )

Post a Comment for "Convert Lists Into 'transposed' List"