Introduction to “PowerShell Scripting guide to Python: Data Structure”

This is the third article in “PowerShell Scripting guide to Python” blog series and we are covering Data Structure today.

If you are new please refer to the part-1 of this blog series for Python prerequisites and the introduction of this series

What has been covered already?

Till now we’ve covered the following topics…

Overview

In this article, we’d be covering following topics –

Arrays, List and ArrayList Creating Array, lists, and ArrayLists Empty lists Adding elements Reversing array elements assigning array elements to multiple variables Accessing elements of the array using indexing Accessing elements of the array using negative indexing Find length the of an array Modifying elements using index Append/insert elements at an index Removing elements of an array Array Slicing Two-dimensional Array\list

Tuples Creating Tuples Use cases

Hash tables and Dictionaries Hashtable with integer keys Accessing elements of a dictionary Adding key-value pairs or replace any existing key Get dictionary keys or values Iterating through key-value pairs Deleting a key-value pair Created nested dictionary Accessing nested dictionary items Sorting a dictionary

Sets What is a Set Example Usecase



My Book – PowerShell Scripting to Python

Arrays

PowerShell array is immutable (fixed sized) Data Structure that can hold heterogeneous elements. Arrays in Powershell are implicitly typed, but in case you want to create a strongly typed array, cast the variable as an array type, such as string[], long[], or int32[]

Python doesn’t have a native array Data Structure, hence Arrays are supported by the array module and need to be imported before using them.

The elements stored in an array are restricted or constrained by their data type are Homogeneous in nature. That means a character array can only store character elements.

The data type is specified using a type code during the array creation, which is a single character like one of the following-

Example –

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 efficient than arrays (* depends on use cases)



ArrayList (.Net)

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



Lists

A list in Python is just an ordered collection of heterogeneous item types and is dynamically(size can change) mutable – add, delete, insert

Key Pointers Python List Mutable Heterogeneous or homogenous items hence can be used as Arrays Loosely typed Built-in into Python Generally comparatively slower than arrays (* depends on use cases)



Operating with PowerShell Array[List] vs Python Lists

Finding Length PowerShell Arrays have a property ‘Length’ which can be accessed using the (‘.’) Dot operator to get the Length (number of elements) of the array Whereas, Python Array’s length is calculated using the Built-in function called len()

Indexing and access/modify elements PowerShell and Python are Zero indexed , that means the first element of a PowerShell Array or Python List has index 0. Array/List index can be used in square brackets [index] to access and modify elements like in the following examplesModifying an element is technically Variable assignment to an array/list index.

Insert/Add elements Adding an element in PowerShell ArrayList is done by .add() method , whereas element insertion is achieved by the .insert() method in both PowerShell and Python. Python List uses .append() method to add elements at the end (last index) of the List.

Reverse elements PowerShell [Array] Type accelerator has a built-in method to reverse() the elements of an array . Similarly, Python has a reversed() method which can reverse the order of List elements.

One to Many: Array to the Variable assignment PowerShell and Python both offers One to Many: Array/List to variable assignment

Remove elements PowerShell and Python both allows you to remove Array/List elements at a specific index, and by value, following are some examples

Array/List slicing Array slicing in PowerShell and Python is just like String Slicing which we covered in previous blog post. PowerShell syntax- array[startindex..endindex] Python syntax- array[startindex:end:step] Here, end = EndIndex+1 step = Increments

Multi Dimensional Array/List Python and PowerShell both enable you to create multi-dimensional arrays and syntax to create one is exactly same.

Tuples