Dynamic Arrays in Python

Laine G
3 min readApr 27, 2020
Image Credit: stillsmybeatingheart.tumblr.com

A dynamic array is, at least in the Python programming language, more commonly known as a list. Because of it’s default functionality, it’s easy to take it’s ease of use for granted. It has the advantage of being easily editted without special memory allocation provisions. It’s also more commonly known as a list.

Declaring a list is simple in Python and requires no more than the following syntax:
list_name = []

The thing that makes dynamic arrays special and distiguishes them from regular arrays, is that they automatically requisition additional space they require in memory, free of the need for user specified memory allocation.

Common ways of manipulating data in a dynamic array, also known as it’s key features, follow below:

  • ADD

Suppose we want to create an array containing weblinks to all of the dance moves contained in Napoleon Dynamite’s famous dance routine. We already have the first of said dance moves, seen at the top of this article.

Napoleon Dynamite, dancing
Image Credit: Center for the Performing Arts

Here’s another. We’ll call it move_2 for reference, and the first one, move_1. Each item in the list will be a string that contains the web address or url where we can find the image.

list.append(new_list_item)
Screenshot by Author

In the scenario where we start with an empty list, new items are added using the built in python method, append(). When the code above is run in the terminal, the url string is gathered from an input() query.

  • DELETE
Image credit: uk.funzing.com

Next, maybe we decide we don’t like some of the moves in the dance routine and don’t want to include them in the list. There are at least a couple of different built in python methods we can use to remove the unwanted item from the list, one of which is the remove() method. Another is pop(). Yet another is to redefine the entire list, excluding the unwanted item.

dance_moves = [move_1, move_2]

  • RESIZE

Automatic Resizing of Memory Allocation

Whenever the length of a dynamic array exceeds a certain boundary, Python automatically increases the size of the array. The threshold at which this increased memory allocation occurs changes as the array size increases. Begining with an array of zero elements, when the array’s length is increased by one, the allocated memory for the array doubles, following a (0, 4, 8, 16, …) pattern. This is to say that as the length of the array continues to increase, the memory allocation for the array will double again when the array’s length increases from four to five, from eight to nine, and again when it increases from 15 to 16, and so on.

Table showing the increase of memory allocation in relation to array length
Image credit: HARSHIT SATYASEEL

The thresholds at which the memory allocation changes are calculated using what is called a geometric progression. Following this pattern, we can deduce that the memory allocation will increase again when the array length increases from 32 to 33.

References and Credits

Make School; CS1.3 Core Data Structures and Algorithms
https://www.geeksforgeeks.org/how-do-dynamic-arrays-work/
https://www.technotification.com/2018/08/dynamic-arrays-python-programming.html
https://en.wikipedia.org/wiki/Dynamic_array

Napoleon Dynamite dance images:
https://thecenterpresents.org
https://stillsmybeatingheart.tumblr.com

--

--