In this tutorial, you’ll learn how to use Python to transpose a list of lists, sometimes called a 2-dimensional array. By transposing a list of lists, we are essentially turning the rows of a matrix into its columns, and its columns into the rows. This is a very helpful skill to know when you’re learning machine learning. Show
In this tutorial, you’ll learn how to do this using for-loops, list comprehensions, the The Quick Answer: Use Numpy’s
What Does it Mean to Transpose a Python List of Lists?Transposing arrays is a common function you need to do when you’re working on machine learning projects. But what exactly does it mean to transpose a list of lists in Python? We can think of sublists in our list of lists in Python as the rows of a matrix. When we transpose a list of lists, we essentially turn the rows of our matrix into the columns, and turn the columns into the rows. Let’s take a look at what this looks like visually: original = [ [1,2,3], [4,5,6], [7,8,9] ] transposed = [ [1, 4, 7], [2, 5, 8], [3, 6, 9] ] Now let’s get started at learning how to transpose a two-dimensional array in Python. Transpose a Python List of Lists using NumpyPython comes with a great utility, One of these operations is the transposing of data, using either the Before we can use any of these methods, however, we need to convert our list or our list of lists to a numpy array. Let’s see how we can use numpy to transpose a list of lists: # Transpose a list of lists in Python using numpy import numpy as np list_of_lists = [ [1,2,3], [4,5,6], [7,8,9] ] array = np.array(list_of_lists) transposed_array = array.T transposed_list_of_lists = transposed_array.tolist() # This is the same as: transposed = np.array(list_of_lists).T.tolist() print(transposed_list_of_lists) # Returns # [ # [1, 4, 7], # [2, 5, 8], # [3, 6, 9] # ] From the example above, we can see how easy it is to use numpy to transpose a Python list of lists. What we did was:
In the next section, you’ll learn how to transpose a two-dimensional array using the built-in
Want to learn how to transpose a Pandas Dataframe? Check out my in-depth tutorial with easy to follow along examples here. Use the Zip Function to Transpose a List of Lists in PythonLet’s
now take a look at how we can use the built-in Now, this might sound a tad confusing, and it can be easier to see it in action than to understand it in theory, right off the bat. So let’s take a look at how we can do transpose a list of lists using the # Transpose a list of lists in Python using numpy import numpy as np list_of_lists = [ [1,2,3], [4,5,6], [7,8,9] ] transposed_tuples = list(zip(*list_of_lists)) # This looks like: [(1, 4, 7), (2, 5, 8), (3, 6, 9)] transposed = [list(sublist) for sublist in transposed_tuples] print(transposed) # Returns # [ # [1, 4, 7], # [2, 5, 8], # [3, 6, 9] # ] Let’s take a look at what we’ve done here:
In the next section, you’ll learn how to use the Want to dive a little deeper into the Sometimes, when working with Python lists of lists, you’ll encounter sublists that are of different lengths. In these cases, the Because of this, there are many times that we need to rely on the Again, this is much easier explained with an example, so let’s dive right in: # Transpose a list of lists in Python using itertools from itertools import zip_longest list_of_lists = [ [1,2,3], [4,5,6], [7,8,9,10] ] tranposed_tuples = zip_longest(*list_of_lists, fillvalue=None) transposed_tuples_list = list(tranposed_tuples) print(list(transposed_tuples_list)) # This returns: [(1, 4, 7), (2, 5, 8), (3, 6, 9), (None, None, 10)] # To create a list of each item: transposed = [list(sublist) for sublist in transposed_tuples_list] print(transposed) # Returns # [ # [1, 4, 7], # [2, 5, 8], # [3, 6, 9], # [None, None, 10] # ] We can see here, that a fourth row has been added. We used the In the next two sections, you’ll learn two more methods, which are more naive implementations using for-loops and list comprehensions, in order to transpose lists of lists. Transpose a Python List of Lists using a For LoopPython for-loops as incredible tools to help you better understand how some algorithms may work. While they may not always be the most efficient way of getting something done, they can be easy to follow and document in terms of what your code is doing exactly. Let’s see how we can implement a Python for loop to transpose a two dimensional array of data: # Transpose a list of lists in Python using for loops list_of_lists = [ [1,2,3], [4,5,6], [7,8,9,10] ] transposed = list() for i in range(len(list_of_lists[0])): row = list() for sublist in list_of_lists: row.append(sublist[i]) transposed.append(row) print(transposed) # Returns # [ # [1, 4, 7], # [2, 5, 8], # [3, 6, 9] # ] What we’ve done here is the following:
In the next section, you’ll learn how to turn this for-loop implementation into a Pythonic list comprehension. Want to learn more about how to write for loops in Python? This tutorial will teach you all you need to know, including some advanced tips and tricks! Use a List Comprehension to Transpose a List of Lists in PythonIn many cases, a Python for loop can be turned a very abbreviated list comprehension. While we increase the brevity of our code, we may sacrifice the readability of our code. Let’s take a look at how we can transpose a list of lists using list comprehensions: # Transpose a list of lists in Python using a list comprehension list_of_lists = [ [1,2,3], [4,5,6], [7,8,9,10] ] transposed = [[row[i] for row in list_of_lists] for i in range(len(list_of_lists[0]))] print(transposed) # Returns # [ # [1, 4, 7], # [2, 5, 8], # [3, 6, 9] # ] We can see here that we’ve saved quite a bit of space. We’ve not had to initialize two empty lists. That being said, this can be a bit more difficult to follow along with, especially as it’s much more difficult to provide guiding comments for each step of the way. Want to learn more about Python list comprehensions? Check out my in-depth tutorial on them here. More of a visual learner? You’ll find an in-depth video on the topic inside the tutorial. ConclusionIn this post, you learned how to transpose a Python list of lists, or a
2-dimensional array. You learned how to do this using the popular data science To learn more about the How do I print a list side by side in Python?You can use the zip() function to join lists together. The zip() function will iterate tuples with the corresponding elements from each of the lists, which you can then format as Michael Butscher suggested in the comments. Finally, just join() them together with newlines and you have the string you want.
How do you print a list of elements on the same line in Python?When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”.
How do you print an item in a list in a list Python?Use * Operator to Print a Python List. Another way to print all of the contents of a list is to use the * or "splat" operator. The splat operator can be used to pass all of the contents of a list to a function.
How do I print a nested list without brackets?use asterisk '*' operator to print a list without square brackets.
|