Introduction

Now the dynamic part of programming starts, python comes with built-in data structure to organize the data type depending on the need and building more complex structures. Data structure can be classified as mutable and no mutable, ordered and non-ordered, and those that accept repleted elements.

Data structures

Depending on the situation, Python provides structures that allow you to access data more efficiently. For programs, accessing data is like searching for a book in a library shell, where you have to look through all the books to find the one you want, but instead of book shells is the memory allocation.

Python provides by default some data structures, they can be classified in mutable and nonmutable.

Let’s start with the mutable.

List

The list are not homogeneous dynamic arrays, which means that it can store different data types, and they can be modifiable after being created (add, remove, update, …). As vectors in CPP they are allocated in memory closely for fast access.

Defining a list it can be in two ways:

  • list() function.
  • Writing the variables between brackets separated by commas.

1
2
3
4
a = [23, 45, 12, 67, 34]
a.append(89)
a.remove(45)
print(a)
output:
1
[23, 12, 67, 34, 89]

MethodDescription
append()allow adding new variables to the list.
remove()removes the firs item at the specified value.
pop()removes the element at the specified position.
clear()Removes all the elements from the list.
copy()Returns a copy of the list.
count()Returns the number of elements with the specified value.
extend()Add the elements of a list (or any iterable), to the end of the current list.
index()Returns the index of the first element with the specified value.
insert()Adds an element at the specified position.
reverse()Reverses the order of the list.
sort()Sorts the list.

Tuple

A tuple is an object collection separated by commas. In some ways, a tuple is similar to a list in terms of indexing, nested objects, and repetition, but a tuple is immutable, unlike list that are mutable, as list they are capable of storing all kind of variable.

Defining a list it can be in two ways:

  • tuple() function.
  • Writing the variables between parentheses separated by commas.

1
2
a = (23, 45, 12, 67, 34)
print(a)
output:
1
(23, 12, 67, 34, 89)

MethodDescription
count()Returns the number of times a specified value occurs in a tuple.
index()Searches the tuple for a specified value and returns the position of where it was found.

Set

A set is an unordered collection of unique elements. It is mutable, meaning you can add or remove elements (only regular sets), and it supports mathematical operations like union, intersection, and difference, they do not support duplicate variables.

Defining a list it can be in two ways:

  • set() function.
  • Writing the variables between braces separated by commas.

1
2
a = {23, 45, 12, 67, 34}
print(a)
output:
1
{23, 12, 67, 34, 89}

To remove duplicates from lists, converting the list to set is a quick and efficient way to do so.

1
2
3
a = [1, 2, 2, 3, 4, 4, 5]
u = set(a)
print(u)
output:
1
(1,2,3,4,5)

MethodshortcutDescription
add()Adds an element to the set.
clear()Removes all the elements from the set.
copy()Returns a copy of the set.
difference()-Returns a set containing the difference between two or more sets.
difference_update()-=Removes the items in this set that are also included in another, specified set.
discard()Remove the specified item.
intersection()&Returns a set, that is the intersection of two other sets.
intersection_update()&=Removes the items in this set that are not present in other, specified set(s).
isdisjoint()Returns whether two sets have a intersection or not.
issubset()<=Returns True if all items of this set is present in another set.
<
issuperset()>=Returns True if all items of another set is present in this set.
>Returns True if all items of another, smaller set is present in this set.
pop()Removes an element from the set.
remove()Removes the specified element.
symmetric_difference()^Returns a set with the symmetric differences of two sets.
symmetric_difference_update()^=Inserts the symmetric differences from this set and another.
union()Return a set containing the union of sets.
update()=Update the set with the union of this set and others.

Set operations

Sets are ideal for performing mathematical operations like union, intersection, and difference, which are useful in fields like data analysis, database management, and computational biology.

1
2
3
4
5
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print("Union:", a | b)
print("Intersection:", a & b)
print("Difference:", a - b)
Output:
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Difference: {1, 2}

Dictionary

Dictionary are an ordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element. Dictionaries store data in key: value pairs. They are ideal for fast lookups and representing structured data.

Defining a list it can be in two ways:

  • dict() function.
  • Writing the key value follow for columns and the variable between braces separated by commas.

1
2
3
4
5
6
7
thisdict = {
  "brand": "Ford",
  "electric": False,
  "year": 1964,
  "colors": ["red", "white", "blue"]
}
print(thisdict)
Output:
1
{'brand': 'Ford', 'electric': False, 'year': 1964, 'colors': ['red', 'white', 'blue']}

MethodDescription
clear()Removes all the elements from the dictionary.
copy()Returns a copy of the dictionary.
fromkeys()Returns a dictionary with the specified keys and value.
get()Returns the value of the specified key.
items()Returns a list containing a tuple for each key value pair.
keys()Returns a list containing the dictionary’s keys.
pop()Removes the element with the specified key.
popitem()Removes the last inserted key-value pair.
setdefault()Returns the value of the specified key. If the key does not exist: insert the key, with the specified value.
update()Updates the dictionary with the specified key-value pairs.
values()Returns a list of all the values in the dictionary.