Search⌘ K
AI Features

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 3.5
## creating arrays
$array = 1,2,3,4,5 # homogeneous array
Write-Host $array
$array = 1, 2, 'text', 1.5 # heterogeneous array
Write-Host $array
$array.gettype() # get data type of the array
[int[]] $array = 1,2,3,4,5 # String typed [int[]] array
Write-Host $array
## Strongly typed array can cast different data types of elements to the defined data type
## if the conversion is possible like [double] to [int] or [char] to [int]
[int[]] $array = 1,2.1,3,4.3,5,[char]'a'
Write-Host $array
## throws error 'Cannot convert value "text" to type "System.Int32".'
## because [string] can't be type casted to [int]
[int[]] $array = 1, 2, 'text', 1.5

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
Python 3.5
## import the module, since Array is not a native data structure
import array
a = array.array("I",[1,2,3,4,5]) # homogeneous, strong typed array
print(type(a)) # get data type of the array
## not implicitly type casted, you've to explicitly typecast elements with the array data type
a = array.array("I",[1,2,3,int(4.3),5])
## operations on array
a.insert(1,7) # inserting elements
print(a)
a.pop(3) # delete and return an element
print(a)
a.reverse() # reverse the array
print(a)
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 .NET type 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.

Python 3.5
## arralist
$names = New-Object System.Collections.ArrayList
[void] $names.Add('Prateek')
[void] $names.Add('Singh')
[void] $names.Add('Prateek')
[void] $names.Add('Singh')
$names.IsFixedSize # output:False

Key Pointers

  • PowerShell (.NET) ArrayList
  • Mutable
  • Homogeneous or Heterogeneous
  • Loosely typed
  • Built-in