The length of 2d array in python is the number of rows in it. Generally 2d array has rows with same number of elements in it.
Consider an example, array = [[10,20,30], [40,50,60]].
Length of array is 2.
Number of rows in 2d array
Use len[arr] to find the number of row from 2d array. To find the number columns use len[arr[0]]. Now total number of elements is rows * columns.
Example:
Consider a 2d array arr = [[20,30,40], [50,60,70]].
Here len[arr] will return 2.
len[arr[0]] will return 3, as it has three columns.
Program:
array = [[100,200,300], [400,500,600]] #get the number of rows rows = len[array] #get the number of columns cols = len[array[0]] print['Length is', rows] print['Number of columns', cols] print['Total number of elements', rows * cols]
Output:
Length is 2 Number of columns 3 Total number of elements 6
Find length of 2d array using numpy
To find the length of 2d array we can use numpy.shape. This function returns number of rows and columns.
Program:
import numpy as numpy arr_representation = numpy.array[[[60, 70],[80, 90],[100, 200]]] print['Number of [rows,cols]', numpy.shape[arr_representation]]
Output:
Number of [rows,cols] [3, 2]
A two-dimensional array can be represented by a list of lists using the Python built-in list
type.
Here are some ways to swap the rows and columns of this two-dimensional list.
- Convert to
numpy.ndarray
and transpose withT
- Convert to
pandas.DataFrame
and transpose withT
- Transpose with built-in function
zip[]
It is easier to use NumPy and pandas, but if you don't want to import NumPy or pandas just for transposition, you
can use the zip[]
function.
The original two-dimensional list is defined as follows:
import numpy as np
import pandas as pd
l_2d = [[0, 1, 2], [3, 4, 5]]
Convert to numpy.ndarray and transpose with T
Create a NumPy array ndarray
from the original 2D list and get the transposed object with the T
attribute.
If you want a list
type object, convert it to a list
with the tolist[]
method.
- Convert numpy.ndarray and list to each other
arr_t = np.array[l_2d].T
print[arr_t]
print[type[arr_t]]
# [[0 3]
# [1 4]
# [2 5]]
#
l_2d_t = np.array[l_2d].T.tolist[]
print[l_2d_t]
print[type[l_2d_t]]
# [[0, 3], [1, 4], [2, 5]]
#
In addition to the T
attribute, you can also use the transpose[]
method of ndarray
and the numpy.transpose[]
function. Please refer to the following article for details such as processing for multi-dimensional arrays more than three dimensions.
- NumPy: Transpose ndarray [swap rows and columns, rearrange axes]
Convert to pandas.DataFrame and transpose with T
Create pandas.DataFrame
from the original 2D list and get the transposed object with the T
attribute.
If you want a list
type object, get numpy.ndarray
with the values
attribute and convert it to list
with the tolist[]
method.
- Convert pandas.DataFrame, Series and list to each other
df_t = pd.DataFrame[l_2d].T
print[df_t]
print[type[df_t]]
# 0 1
# 0 0 3
# 1 1 4
# 2 2 5
#
l_2d_t = pd.DataFrame[l_2d].T.values.tolist[]
print[l_2d_t]
print[type[l_2d_t]]
# [[0, 3], [1, 4], [2, 5]]
#
Transpose with built-in function zip[]
You can transpose a two-dimensional list using the built-in function zip[]
.
zip[]
is a function that returns an iterator that summarizes the multiple iterables [list
, tuple
, etc.].
- zip[] in Python: Get elements from multiple lists
In addition, use *
that allows you to unpack the list and pass its elements to the function.
- Unpack and pass list, tuple, dict to function arguments in Python
l_2d_t_tuple = list[zip[*l_2d]]
print[l_2d_t_tuple]
print[type[l_2d_t_tuple]]
# [[0, 3], [1, 4], [2, 5]]
#
print[l_2d_t_tuple[0]]
print[type[l_2d_t_tuple[0]]]
# [0, 3]
#
Elements are tuple
. If you want to make list
, use list[]
and list comprehensions.
- Convert list and tuple to each other in Python
- List comprehensions in Python
l_2d_t = [list[x] for x in zip[*l_2d]]
print[l_2d_t]
print[type[l_2d_t]]
# [[0, 3], [1, 4], [2, 5]]
#
print[l_2d_t[0]]
print[type[l_2d_t[0]]]
# [0, 3]
#
The detail is as follows.
The elements of the list are expanded with *
, the expanded elements are combined with the zip[]
function, and tuple
is converted to list
in the list comprehension.
print[*l_2d]
# [0, 1, 2] [3, 4, 5]
print[list[zip[[0, 1, 2], [3, 4, 5]]]]
# [[0, 3], [1, 4], [2, 5]]
print[[list[x] for x in [[0, 3], [1, 4], [2, 5]]]]
# [[0, 3], [1, 4], [2, 5]]