Data Structures Cont'd: A Beginner's Perspective

Data Structures Cont'd: A Beginner's Perspective

Tuples, Sets & Dictionaries

Tuples

Tuples are like lists, only that they store immutable and ordered data types. They can be used when dealing with information or data that is closely related and often used together e.g., latitudes and longitudes.

More often, programmers omit using parentheses () if they do not provide further definition to the code. Tuples, like lists, can be accessed by their indices e.g,

dimensions = 3, 4, 5

print ("length:" , dimensions[0])

>>> 3

Noteworthy, trailing commas are used to define tuples e.g.,

variable = 3,

print (type(variable))

>>> <class 'tuple'>

When the comma is removed, the code will return an int as illustrated below:

variable = 3

print (type(variable))

>>> <class 'int'>

Sets

They are a mutable and unordered collection of unique elements. An example of a set is a google form response for vehicles models that will be present at a car meet in Eldoret.

tuples.png

print (set(vehicle_types)) returns an unordered set of vehicle_types without any duplicates.

>>> {'volkswagen', 'mazda', 'nissan', 'mercedes', 'toyota', 'mitsubishi', 'honda'}

print (len(set(vehicle_types))) returns the number of unique vehicles present. In this case, it is 7.

Additionally, we can check for the presence of a vehicle type or its absence using in e.g

print ("toyota" in vehicle_types) returns True whereas print ("suzuki" in vehicle_types) returns False

Set Methods
  1. add() - adds an element to the set
  2. pop() - sets are unordered so this methods removes an element at random
  3. remove() - removes a specified element from the set. However, removing elements not in a set returns a KeyError. Using discard() bypasses this error although it is advisable to first check whether an element is in the list using in
  4. clear() - clears all elements from a set
  5. frozenset() - returns an immutable set. Yes, I recall sets being mutable but try this out just for the kick of it :)

Dictionaries and Identity Operators

Dictionaries

According to my understanding, dictionaries store pairs of elements i.e keys and their values. They are defined using curly braces {}. For example:

dictionaries.png print(type(ages)) returns class 'dict'

print(ages["Brian"] returns 27

In the above example, the value "Brian" has been accessed by using square brackets. To add elements in a dictionary, I tried the following:

ages["Maminka"] = 55

print (ages)

>>> {'Tracie': 29, 'Brian': 27, 'Anne': 25, 'Maminka': 55}

Values in a dictionary can also be accessed by using the get() method. For example,

print (ages.get("Maya"))

The above syntax returns None as the default value. However, it is possible to pass another argument within the get() method.

print (ages.get("Maya", "The age specified is not available"))

Further, we can check if an element is within a dictionary using is and is not. These are referred to as Identity Operators.

x = ages.get("Allan")

is_null = x is None

print (is_null)

>>> True

Using is not we get:

not_null = x is not None

print (not_null)

>>> False

Please feel free to reach out or comment on anything that I have written above. Additional tips and tricks are always welcome!