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.

    Data Structures

  • 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

    Data Structures

  • 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

    Data Structures

  • Multi Dimensional Array/List

    Python and PowerShell both enable you to create multi-dimensional arrays and syntax to create one is exactly same.


Tuples

Tuples are immutable (constant) Data Structure support in Python and PowerShell, unlike lists or array lists which can be modified.

Once a Tuple is defined you cannot delete, add or edit any values inside it.This can be very helpful in use cases where you might pass the control to someone but you do not want them to edit or manipulate data in your data structure, but rather maybe just perform operations separately in a copy of this Data Structure.

PowerShell –

Defining Tuples in PowerShell requires you to use .NET classes or [System.Tuple] type accelerator

Python –

Whereas, use () parenthesis to define a tuple in Python.


Sets

Sets are a collection of distinct (unique) objects that only hold unique values in the dataset.

A set is an unordered collection but a mutable one(can be modified)

 

Sets are very helpful when going through a huge dataset and want to find Intersection, union and other data sets.

PowerShell utilizes .Net class System.Collections.Generic.HashSet<T> to create a HashSet

Whereas, Python has an inbuilt method set() to define sets.

Data Structures


HashTables and Dictionaries

Hashtables or Dictionaries are Data Structure that are made up of Key-Value pairs, ‘key’ is used to identify an item which holds a ‘value’

 

PowerShell-

 

Define PowerShell hashtables using ‘@’ following open and closing ‘{}’ braces, and semi-colon to separate the key-value pairs

Python-

Whereas, in Python, you can define Dictionaries using open and closing ‘{}’ braces and comma (,) to separate the key-value pairs


HashTables/Dictionary operations

  • Accessing Key-Value pair

    A key value pair of hashtable\dictionary in PowerShell and Python is accessed by name followed by key inside ‘[]‘ brackets

    PowerShell-

    Python-

  • Adding Key-Value pair

    Powershell uses .add() method to add key-value pairs, whereas in Python you can directly assign a value to keys like variable assignment

    PowerShell-

    Python-

    Data Structures

  • Find Keys and Values

    Find all keys or values use ‘key’ and ‘value’ property of hashtable in PowerShell, whereas key() and value() methods are used in Python for same.

    PowerShell-

    Python-

  • Iterating through key-value pairs

    In some usecases you’ll want to iterate the key-value pairs in a hashtable or a dictionary, following are some examples to demonstrate that.

    PowerShell-

    Python-

  • Deleting a key-value pair

  • Create and access nested hashtables

    Just like PowerShell, you can create nested dictionaries [dictionary in a dictionary] in Python

    PowerShell-

    Python-

  • Sorting hashtable

    Sort Hashtables in PowerShell and Dictionaries in Python if required.

    PowerShell-

    Python-

    Data Structures


Hope you enjoyed reading this article and let me know your feedback.

In Part-4, ie, the next article of this blog series, we would be covering Loops and Iterations hence, please stay tuned!


Blog Series

Till now we’ve covered the following topics…


 

signature

Subscribe to our mailing list

* indicates required