Python is a versatile and widely used programming language that allows you to create various types of data structures. Among these, lists are one of the most common structures used to store multiple items in a single variable.
A Fixed-Size List, as the name suggests, is a list that maintains a constant or fixed size even after operations. This article will present a comprehensive guide to help you create a fixed-size list in Python.
Step 1: Importing the Array Module
Python’s array module provides a compact way for defining array-like data structures where all elements are of the same kind. Unlike lists, arrays in Python enforce certain restrictions that will allow us to manage a list with a fixed size. Start with importing the said module:
1 |
import array as arr |
Step 2: Creating a Fixed-Size Array
After importing the array module, you can now create a fixed-size array. To do this, use the array() constructor and specify the type of items in the array using a type code and the elements within square brackets ‘[]’.
1 |
fixed_array = arr.array('i', [1, 2, 3, 4, 5]) |
Step 3: Checking the Initial Size of the Array
To ensure that your array is indeed fixed-size, check the initial size of your array using the buffer_info() method. This method returns a tuple representing the memory address where the array starts and the number of elements in the array.
1 2 |
initial_size = fixed_array.buffer_info() print("Initial size of array: ", initial_size[1]) |
Step 4: Validate the Fixed-Size Property
Now let’s test if adding or deleting elements would affect the size of our array. If it maintains the initial size, then we have successfully created a fixed-size list.
1 2 3 4 5 6 7 8 9 10 11 |
# Try adding an element fixed_array.append(6) after_adding = fixed_array.buffer_info() print("Size of array after adding an element: ", after_adding[1]) #Try deleting an element fixed_array.pop() after_deleting = fixed_array.buffer_info() print("Size of array after deleting an element: ", after_deleting[1]) |
Complete Python Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import array as arr # Creating a Fixed-Size Array fixed_array = arr.array('i', [1, 2, 3, 4, 5]) # Checking the Initial Size of the Array initial_size = fixed_array.buffer_info() print("Initial size of array: ", initial_size[1]) #Validate the Fixed-Size Property # Try adding an element fixed_array.append(6) after_adding = fixed_array.buffer_info() print("Size of array after adding element: ", after_adding[1]) #Try deleting an element fixed_array.pop() after_deleting = fixed_array.buffer_info() print("Size of array after deleting element: ", after_deleting[1]) |
Output:
1 2 3 |
Initial size of array: 5 Size of array after adding element: 6 Size of array after deleting element: 5 |
Conclusion
Even with the modification operations, the size of your array should always return back to its initial size which is how you validate that you have successfully created a fixed-sized list in Python.
This tutorial covered four basic steps to create a fixed-size list using Python’s built-in library, the array module. Now you can utilize this technique to manage your data storage and handling more efficiently.