Arrays and Arraylist
Learn to compare PowerShell arrays and ArrayLists with Python arrays. Understand their mutability, typing, and how to work with these data structures effectively in both languages.
We'll cover the following...
Arrays
A PowerShell array is an immutable (fixed sized) data structure that can hold heterogeneous elements.
Arrays in Powershell are implicitly typed, but if we can cast the variable as an array type if we want to create a strongly typed array, such as string[], long[], or int32[].
Python doesn’t have a native array data structure, but arrays are supported using the built-in module called array. This module needs to be imported before using the data structure. The elements stored in a Python array are restricted by their data type and thus are homogeneous in nature. This means a character array can only store character elements, unlike PowerShell arrays that can hold heterogeneous elements.
To define an array in Python, we have to specify the data type using a type code during the array creation, which is a single character. A number of type codes are listed in the following table.
| Type code | C Type | Python Type | Minimum size in bytes |
|---|---|---|---|
| ‘c’ | char | character | 1 |
| ‘b’ | signed char | int | 1 |
| ‘B’ | unsigned char | int | 1 |
| ‘u’ | Py_UNICODE | Unicode character | 2 |
| ‘h’ | signed short | int | 2 |
| ‘H’ | unsigned short | int | 2 |
| ‘i’ | signed int | int | 2 |
| ‘I’ | unsigned int | long | 2 |
| ‘l’ | signed long | int | 4 |
| ‘L’ | unsigned long | long | 4 |
| ‘f’ | float | float | 4 |
| ‘d’ | double | float | 8 |
a = array.array("I",[1,2,3,4.3,5])
This will cause an error in Python.
TypeError: integer argument expected, got float
Key Pointers
-
PowerShell arrays
- Immutable
- Homogeneous or Heterogeneous
- Loosely typed
- Built-in
-
Python arrays
- Mutable
- Homogeneous
- Strictly typed
- Not built-in and have to be imported
- Generally comparatively faster and more efficient than arrays (* though that depends on use cases)
ArrayList (.Net)
We just learned about PowerShell’s default arrays which are very useful due to their flexibility and ability to store heterogeneous objects of any data type, including $null, in a single array.
$array = 'hello world!', 111, 13.14, (Get-Date), $null
However, this flexibility also introduces some drawbacks:
-
We cannot ensure that all elements are of a specific type or should be converted to a single data type, because of the heterogeneous nature of the elements. Since PowerShell has access to
.NETtype system, we can overcome these drawbacks by creating an array that is restricted to the specific type of interest using a cast or type-constrained variable:[int[]] $Array = 1, 2, 3 -
PowerShell arrays are immutable, which means they are of fixed size and, once defined, new elements cannot be added. To overcome this, we should add the new element to the existing elements of the array and assign all elements (new + existing) to an array variable.
# array $fruits = @() # adding elements to arrays # throws an exception "Collection was of a fixed size." # because powershell array variables are immutable and this is an empty array $fruits.Add('apple') $fruits.IsFixedSize # output:True # so when we are assigning an element the size will change, # which is not allowed # adding elements the correct way, this works! # because in the background it creates a whole new array # that includes the new value and then discards the old array. $fruits += 'apple' $fruits += 'banana' $fruits += 'orange'
PowerShell can also use .Net Class: System.Collections.ArrayList, which are mutable, dynamic, and much faster in nature.
Key Pointers
- PowerShell (.NET) ArrayList
- Mutable
- Homogeneous or Heterogeneous
- Loosely typed
- Built-in