Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python transpose

a = [(1, 'John', 1996),
    (2, 'Doodie', 2001),
    (3, 'Doe', 2006)]
b = list(zip(*a))
#[(1, 2, 3),
#('John', 'Doodie', 'Doe'),
#(1996, 2001, 2006)]
'''O(m*n) time complexity, if data >1000, use numpy or pandas'''
Comment

python transpose

l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

t = list(zip(*l))
# t = [(1, 4, 7), (2, 5, 8), (3, 6, 9)] # list with tuples

t = list(map(list, zip(*l)))
# t = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] # list with lists
# t = [*map(list, zip(*l))] works as well
Comment

PREVIOUS NEXT
Code Example
Python :: import python module 
Python :: python list pop equivalent 
Python :: python error 
Python :: python and flask create_app 
Python :: generating random numbers numpy 
Python :: pytest use fixture without running any tests 
Python :: self object 
Python :: sub function python 
Python :: queue class python 
Python :: templates python 
Python :: python iterrows 
Python :: self.assertequal python 
Python :: queryset django 
Python :: when converting from dataframe to list delete nan values 
Python :: sys python 
Python :: python array empty 
Python :: telegram bot carousel 
Python :: python list of possible paths 
Python :: Python fibonacci series (Recursion) 
Python :: using pandas stack and subset to return a dataframe object of highly correated pairs 
Python :: reverse words and swapcase in python 
Python :: pandas read sql generator to dataframe 
Python :: how to send message to specific channel discord/py 
Python :: destroy trigger python 
Python :: write str in a formal way in python 
Python :: treesitter python languages 
Python :: mostFrequentDays python 
Python :: clipping path image using python 
Python :: should i learn c++ or python 
Python :: numpy array values not updateing 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =