How to Find the Shape of a Python Tuple or List

Python tuples and lists are objects that contain multiple values and it is a common task to determine the number of elements (or shape) in a tuple or list object. While this seems simple enough, there is one nuance that makes it a little more complicated.

Shape of a One-dimensional Python Tuple or List

The shape of a simple Python tuple or list can be obtained with the built-in len() function. len() will return an integer that describes the number of objects in the tuple or list.

my_tuple = (1, 2, 4, 3, 2, 5)
len(my_tuple)
Output:
6

This procedure gets more complicated for a tuple of tuples or a list of lists. You can think of these as 2-dimensional tuples or lists. The remainder of this article will break down these special cases.

Shape of Multi-dimensional Python Tuple or List

In Python a multidimensional tuple or list is one that contains another tuple or list as one of its elements. For example, take this tuple, which is composed of three separate tuples.

my_tuple_2d = ((1, 2, 3), (1, 2), (1, 2, 3, 4))

This example shows why we can’t use a simple command like shape to determine the shape of tuple or list like we would do for a numpy array (for the shape of numpy arrays, see this article).

Python tuples and lists are fundamentally different from arrays because they do not have a set number of rows and columns. In other words, a tuple (or list) can have any number of elements at any position. This is shown by the tuple above, which has a length of 3. However, the tuples within `my_tuple_2d’ each have different shapes of 3, 2, and 4, respectively.

To get the shape of each tuple within my_tuple_2d we need to loop through all the elements of my_tuple_2d and query their length individually. Below, I first demonstrate the length of my_tuple_2d, then the length of each of its elements.

len(my_tuple_2d)
Output:
3

As you can see, there are three elements in my_tuple_2d. I can now return the length of each tuple within my_tuple_2d as a list using the following code.

[len(a) for a in my_tuple_2d]
Output:
[3, 2, 4]

It takes an integer and a list to describe the shape of a tuple of tuples or list of lists. The integer give the number of elements in the tuple or list. The list gives the size of each element. my_tuple_2d would be described by 3 and [3, 2, 4].

For a full tutorial that demonstrates how to find the dimensions of any multidimensional list or tuple, check out this article.

Conclusion

As demonstrated list and tuple shapes in Python can be a little bit complicated. Lists and tuples are not arrays are not structures with strict dimensions like arrays. Each element in a tuple or list can have differing lengths, which makes it difficult to concisely describe its shape. On the other hand, this same flexibility makes it possible for tuples and lists to be useful in many different applications.


My Recommended Equipment

Computer: Dell XPS

Mouse: Logitech M557 Bluetooth Mouse

External Hard Drive: Seagate Portable 2TB


This article contains affiliate links. When you click on links in this article Open Source Options may make a commission on any sales. This does not have any impact on the price you pay for products.

Similar Posts