Language: EN


What are and how to use dynamic lists or arrays

LISTS, or dynamic arrays, are data structures similar to ARRAYS, but of variable size. Unlike arrays, in a list we can add or remove elements as needed.

That is, we have the same operations as we would have in an array of fixed size. But they incorporate additional functionality of,

  • Add elements
  • Remove elements

LISTS offer a simple way to store data, allowing us to add, remove, and access elements. This makes them ideal for managing data collections whose structure can change over time.


Frequency with which you will use it🔺🔺
Is mutable✔️
Is ordered✔️
Is indexable✔️
Allows duplicates✔️

When to use a List

The main use of LISTS is working with them. It is your favorite collection to work with collections.

A LISTA has all the functionalities of an ARRAY in addition to allowing you to add and remove objects. Therefore, they are much more powerful and versatile.

When not to use them? In general, it is convenient to avoid them for returning collections between functions. For that, it is better to use a ARRAY of fixed length.

List examples in different languages

Syntax and declaration of a List

The syntax for declaring a list may vary depending on the programming language being used. We are going to see some examples in different languages,

List<int> numeros = new List<int>();
#include <vector>

std::vector<int> numeros;
let numeros = [];
nombres = []

In the previous examples, we create empty lists that can store elements of a specific type, such as integers in the case of C# and C++, and of variable type in the case of JavaScript and Python.

Access and Manipulation of Lists

Once we have declared a list, we can access and manipulate its elements in a similar way to how we would with an array.

Adding elements to a List

numeros.Add(10); // Adds the element 10 to the list
numeros.Add(20); // Adds the element 20 to the list
numeros.push(10); // Adds the element 10 to the list
numeros.push(20); // Adds the element 20 to the list
numeros.push(10); // Adds the element 10 to the list
numeros.push(20); // Adds the element 20 to the list
nombres.append(10)  # Adds the element 10 to the list
nombres.append(20)  # Adds the element 20 to the list

Removing an element from a List

numeros.Remove(10); // Removes the first occurrence of element 10
numeros.RemoveAt(2); // Removes the element at position 2
numeros.erase(numeros.begin() + 2); // Removes the element at position 2
numeros.splice(2, 1); // Removes 1 element starting from position 2
nombres.remove(10)  # Removes the first occurrence of element "10"
del nombres[2]  # Removes the element at position 2

Efficiency of Lists Intermediate

The LISTS share the efficiency parameters with their siblings the ARRAYS. Because, internally, they are generally an array.

Sequential access🟢
Random access🟢
Add at the beginning🔴
Remove at the beginning🔴
Add at the end🟡
Remove at the end🟢
Random insertion🔴
Random removal🔴

However, we now have the option to add and remove elements. Removing an element at the end of the collection is O(1), since we only have to reduce the element count.

Adding an element at the end can be O(1), if the list has available capacity. If it happens to be full, the list will have to be expanded, which is an O(n) operation.

Finally, adding or removing an element at the beginning or in the middle of the collection is O(n), because the list has to shift all the elements to locate / remove the new element.

Read more about collection efficiency read more ⯈

Internal operation

Internally, a LISTA is usually implemented as an object around an ARRAY. This object contains:

  • The data array
  • The list capacity
  • The number of elements

When creating a LISTA, the internal array initializes to a certain size, which is the list capacity.


As we add elements, the list counts the positions of the array we are using.

If at any time the number of elements is equal to the list capacity, a new array of larger size (typically double) is generated, and the elements of the previous array are copied. Finally, the previous array is destroyed.


This is done transparently to the programmer, so we don’t have to worry about memory management.