Double Life of Python List.
Tags: python, programming, beginners, dsa
Published: 2023-10-07 19:34:51

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgmdl6ur5h5rlu5olg5z.png)

An array is a data structure that holds an ordered collection of elements, all of the <u>same data type</u>. 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};
```

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]
```

This mechanism <u>(call by reference)</u> 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 <u>(Call by value)</u>. 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"};
```
## 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
```

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!🙂