DEV Community

Cover image for Double Life of Python List.
Nihal Patel
Nihal Patel

Posted on

Double Life of Python List.

Introduction:

Arrays and lists are fundamental data structures in programming, serving as containers for collections of elements. They provide efficient means of organizing and accessing data. In this article, we will learn about arrays, dynamic arrays, and explore how Python's lists are implemented.

Foundation of Arrays: Homogeneity

geeksforgeeks

An array is a data structure that holds an ordered collection of elements, all of the same data type. Think of it as a series of storage locations, each containing individual data, accessible by an index. However, arrays can only store elements of the same type.

// Creating an array in C++
int myArray[5] = {1, 2, 3, 4, 5};
Enter fullscreen mode Exit fullscreen mode

What if there's a need to store multiple elements with different data types? This is where Python lists come to the rescue. In Python lists, we can store different types of data due to Python's dynamic typing, which allows for flexibility in the types of elements a list can hold.

Python List: Heterogeneity

When we add elements of different types to a Python list, Python internally manages them by storing references to these elements. Each element is stored in a separate memory location, and the list contains references to these memory locations. When we access an element from a Python list using its index, we are not directly fetching the element; instead, Python goes to the specific memory location referenced by the index and retrieves the element stored there.

# Creating a Python list
my_list = [1, 'Hello', 3.14, True]
Enter fullscreen mode Exit fullscreen mode

This mechanism (call by reference) is what enables Python lists to be heterogeneous, allowing them to store various types of data seamlessly. In contrast, with arrays, we can directly access elements using their index (Call by value). So, while it may seem like we are accessing the element when working with Python lists, we are, in fact, accessing the referenced elements themselves, making Python lists a versatile and powerful tool for handling diverse data.


Foundation of Arrays: Static Size Limitation

Arrays are of a predetermined size. Once defined, their size cannot be changed. Arrays are a staple in many programming languages, including C, C++, and Java. Arrays are efficient in terms of memory usage and access times. However, their fixed size can be limiting.

// Java
String[] superHero = {"BATMAN"};
Enter fullscreen mode Exit fullscreen mode

Python Lists to the Rescue:

In Python, when we create a list and start adding elements, Python initially allocates a certain amount of memory for the list. If the list reaches its allocated capacity, Python dynamically increases the size of the list by doubling its capacity. This process involves allocating a larger chunk of memory and copying the elements from the old location to the new one, allowing us to add more elements efficiently.

# Creating a Python list with dynamic resizing
my_list = ['BATMAN', 3, True]
my_list.append(8)  # Automatically adjusts the size
Enter fullscreen mode Exit fullscreen mode

Python repeats this resizing process as needed to accommodate additional elements of any type, making lists versatile containers for varying amounts of data. And this is one of the reasons why Python lists can be slower than arrays in languages like C.

Conclusion:

The choice between arrays and lists, or similar data structures, depends on your specific programming language and the requirements of your application. Arrays are favored for memory efficiency and strict typing, while lists (especially in Python) offer versatility, automatic memory management, and dynamic resizing at the cost of slightly lower performance in some cases.

In summary, understanding the characteristics of arrays and Python lists empowers you to make informed decisions when designing data structures for your programs. If you liked this post give it a like and consider following. Happy coding!🙂

Top comments (0)