Katy shares her tech knowledge and research with anyone who wants to learn the smartest way to use technology.
Length of List in Python
Lists are a data structure commonly used in Python. It's handy to be able to get their lengths. Find out how to find dimensions of arrays and lists.
The function len will return the length of a list. Here's how to use it:
It's that easy! The len function will return the number of elements in that list. Set it equal to another variable to use later in your code.
If you have a multi-dimensional array you can also use len to find the size of a multi-dimensional array, read more on that down below.
First, let's look at an example for a one-dimensional list:
Find Length of a List Example
The above example with the Three Stooges shows the basics of finding the length of a list.
The list "stooges" is defined with three string elements. The function len is called by passing in the "stooges" list. The result is the length of that list: 3.
"List" or "Array"?
If you're new to Python from another language you might not be familiar with lists. Lists are Python's version of an array. They are 1D arrays of any data type and can even be made up of mixed data types.
Lists are indexed in Python just like arrays in other languages. So you can reference a single element in a list by calling out the element number in brackets [ ].
The terms "list" and "array" are often used interchangeably for programmers outside of Python. So when we refer to finding the length of a list or an array, the meaning is the same.
2D Array or Matrix
The function len acts differently with a 2D array or matrix. Calling len on a multi-dimension matrix will return the number of rows in that matrix.
Multi-Dimensional Array Example
In the above example we build a 2D array of values. The array A has 4 rows and 2 columns.
The function len returns a value of 4 when passed the whole array A because that's the number of rows.
To get the number of columns you can pass A (the first row in A) to the function len, so the result will be 2 columns. Note that this will only look at the number of columns in the first row so if you have an uneven array with changing numbers of columns you will need a different approach.
Python allows you to used nested lists, which are lists within lists. Nested lists are another interesting area for using length.
You can index the parent list to specify one list. Call the len function on that list and you will get the number of elements on just that one list.
In the example above we declare a list of scores for 3 players. Those lists are concatenated (or joined) into a single list called "all_scores".
Calling for the length of the 0 index of all_scores gets you the number of elements in playerA's list, which is 3.
The 1 index of all_scores refers to the number of elements in playerB's list, which is just 2.
Remember that Python lists start indexing at 0. So the first row is 0 and the second is 1, etc.
Learn More About Python Lists
© 2019 Katy Medium