Module 5 — Data Structures

A.I Hub
7 min readJul 30, 2023

--

Image by Unsplash

In addition to commonly used data types int, float and str. Python offers data types to store a collection of multiple entries from numbers, alphabets,

alphanumeric, strings and special characters. The four commonly used collection data types provided by python are:

  • List — List is a mutable and ordered collection that allows duplicate entries.
  • Tuple — Tuple is an immutable and ordered collection that also allows duplicate entries.
  • Set — Set is an unordered and unindexed collection that, like real sets prohibits duplicate entries.
  • Dictionary — Dictionary is a mutable, unordered and indexed collection of entries that prohibits duplicate entries.

List

A list is a mutable and ordered collection of elements lists are specified using square brackets in python.

To access any particular element/item of a list we refer its

index number. To print the third item of the list, we type.

print(item_list[2])

Since Python permits negative indexing we can use them to

access elements of a list. As an example, this piece of code prints the third last item of the list.

print(item_list[-3])

To return the second and the third list items, we type this

code.

print(item_list[1:3])

The index of a specific element can be used to change the value of that element.

Conditional statements can be used with a list. The elements

of a list can be checked using an if statement and the keyword

in as follows.

Tuple

A tuple is immutable and ordered collection of items. In Python tuples are specified using round brackets (). This code creates a tuple.

Elements of a tuple can be accessed using [ ].

Similar to lists we can use negative indexing and a range of indexing. Since tuples are immutable, the values present in a tuple are not changed once it is created. However, we can use loops to go through the elements of a tuple. The keyword in can be used to determine if a specified element is present in a tuple. The method len() finds out the number of items present in a tuple. The + operator can be employed to join two or more tuples together. Finally to delete a tuple use the statement del py_stat.

The tuple method count() returns the number of times a particular value appears in a tuple.

py_stat2 = (‘Python’, ‘has support for’, ‘statistics’)

print(py_stat2.count(‘stat’))

print(py_stat2.count(‘statistics’))

The tuple method index() is used to search the tuple for a particular value. It returns the position where the specified value is found.

A ValueError is printed if the value to be indexed for does not

exist in the tuple.

Set

A set is an unindexed and unordered collection of items.

Python specifies sets using curly brackets { }.

No index is linked to set items because sets are unordered. Never the less a loop can be used to go through the set elements.

The printed output follows no order. The keyword in is used to

check if a particular value is present in a set.

Once a set is created its elements cannot be changed. But new elements can be added. The method add() adds a single element to a set.

The output of the aforementioned program displays items without any order. The method update() adds multiple elements to a set.

The item ‘sheep’ appears once in the output because set do not allow duplicate entries. To find the number of elements of a set we use the method len(my_animals).

To remove an element in a set, we use either the method remove() or the method discard(). For instance, we remove
“dog”.

Two or more sets can be joined together using the method union(). Alternatively, the method update() can be used to insert elements from one set into another.

the method pop( ) removes the last item from a set. The method clear ( ) empties the set and the keyword del before the name of the set deletes the set completely.

Dictionary

A dictionary is a mutable, unordered and indexed collection of items. A dictionary in Python has a key:value pair for each

of its elements. Dictionaries retrieve values when the keys are

known. To create a dictionary we use curly braces { } and put key: value elements inside these braces where each pair is separated from others by commas.

The dictionaries require the keys to be unique and immutable

string, number or tuple. The values on the other hand can be of any data type and can repeat. Square brackets are used to refer to a key name to access a specified value of a dictionary.

A message - None is displayed if we try to access a non-existent

key.

We get the error: KeyError: ‘address’ to indicate that the

key ‘address’ does not exist when we run print(py_stat_dict

[‘address’]). The value of an element can be changed by referring to its key.

A for loop can be used to go through a dictionary, it returns

keys of the dictionary.

The same output is obtained when we use this code.

To check if a key exists within a dictionary, we employ a
conditional if statement.

We add a new element to a dictionary. For this, a new key is

used and a value is assigned to this key.

The method pop() can be used to remove a specific element.

The keyword del removes the whole dictionary when we use

del py_stat_dict. The method clear() deletes all the elements

of a dictionary.

The method len(dictionay_name) is used to print the number

of key:value pairs present in the dictionary.

A dictionary cannot be copied by a simple assignment such as py_stat_dict2 = py_stat_dict. It is because py_stat_dict2 is just a reference to the original dictionary py_stat_dict. Whatever changes are made to py_stat_dict are automatically made to py_stat_dict2 as well. To copy the elements of a dictionary we use the method copy() for doing this.

Conclusion

In this step by step guide, we will walk you through the data structures in python and along with that we also covering most important functions that we used within data structure. In this particular guide, we will understanding the concept of different data structures that is basically matters when we talk about to handle large volume of data bundles.

--

--

A.I Hub
A.I Hub

Written by A.I Hub

We writes about Data Science | Software Development | Machine Learning | Artificial Intelligence | Ethical Hacking and much more. Unleash your potential with us

No responses yet