DEV Community

Anthony Lopez
Anthony Lopez

Posted on

Data Structures In Python : The Building Blocks Of Software Systems

Why This Topic?

Traversing in the world of coding, a programmer should consistently strive to cultivate their skills and reinforce their knowledge by first understanding the what and why of code. Quite often the struggles that arise and serve as obstacles for a programmer are disguised as simple questions to doubt the reasoning and logic behind the purpose of writing their code. However, these obstacles can easily dissipate when one is armed with the knowledge of what they are dealing with. A common goal of a programmer would relate to their skills dealing with hardware. This could consist of abilities like database management, statistical analysis, and software knowledge.

Database management refers to the actions one takes to manipulate and control data to meet necessary conditions throughout the entire data lifecycle. Database management requires one to have strong skills with data structures and algorithims. These skills compound the strength of a programmer and that is because data structures serves as one of the building blocks of large software systems. Understanding how to use this knowledge is critical, because it is what can lead to building software that is efficient and optimized.

Data Structures In Python

Organizing, managing, and storing data is more than what's likely to be expected from a programmer. However, with the existence of data structures, programmers can store collections of data, relate them and perform operations on them accordingly.

Python has built-in support for data structures, and these structures are known as List, Dictionary, Set, and Tuple. Python also allows the programmer to create their own data structures. Data structures created by the programmer are commonly known as Stack, Queue, Tree, Linked List, Graph, and Hashmap. These data structures enable the programmer to have full control over the functionality of said structures. For today's subject, the focus will be on the built-in data structures of Python.

Lists in Python are used to store data of different types in a sequential manner. Each element of the list is assigned an address, which is called an index. The value for the first element starts with an index of 0, then goes on and on until the very last element. This is known as positive index. On the flip side, there is negative index, where the value for the last element starts with -1, and goes on and on until the very first element. Let's take a look at an example of a list.

my_list = [] 
#this creates a empty list, or in other words, a list with no elements

my_list = ['duck', 'buck', 'goose'] 
#this creates a list of 3 elements
Enter fullscreen mode Exit fullscreen mode

Since my_list has 3 elements, going by positive index, duck would have be at index 0, buck would be at index 1, and goose would be at index 2. Going by negative index, goose would be at index -1, buck would be at index -2 , and duck would be at index -3.

To manipulate the data within the list, the append(), extend(), and insert() functions can be used.

Adding Elements

my_list = ['duck', 'buck', 'goose']
#this creates a list of 3 elements

my_list.append(['chick', 'chicken'])
#adds as a _single_ element

print(my_list)
['duck', 'buck', 'goose', ['chick', 'chicken']]
Enter fullscreen mode Exit fullscreen mode

The append() function adds all the elements passed into it as a single element.

my_list = ['duck', 'buck', 'goose']
#this creates a list of 3 elements

my_list.extend(['hippo', 'lion'])
#adds as different elements

print(my_list)
['duck', 'buck', 'goose', 'hippo', 'lion'] 
Enter fullscreen mode Exit fullscreen mode

The extend() function adds the elements one-by-one into the list.

my_list = ['duck', 'buck', 'goose']
#this creates a list of 3 elements

my_list.insert(2, 'bat')
#this adds the element starting from index 2

print(my_list)
['duck', 'buck', 'bat', 'goose']
Enter fullscreen mode Exit fullscreen mode

The insert() function adds the element passed to the index value and increase the size of the list as well.

Deleting Elements

In contrast, instead of adding data, the del keyword, pop() function, remove() function and clear() function can delete data.

my_list = ['duck', 'buck', 'goose']
#this creates a list of 3 elements

del my_list[2]
#this deletes the element at index 2.

print(my_list)
['duck', 'buck']
Enter fullscreen mode Exit fullscreen mode

The del keyword deletes the element passed to the index value and shortens the list as well.

my_list = ['duck', 'buck', 'goose']
#this creates a list of 3 elements

v = my_list.pop(1)
#pops the element at index 1 and gives it back

print('Popped Element: ', v , 'List Remaining: ', my_list)
Popped Element: 'buck' List Remaining: ['duck', 'goose']
Enter fullscreen mode Exit fullscreen mode

The pop() function takes the index value and gives the element back.

my_list = ['duck', 'buck', 'goose']
#this creates a list of 3 elements

my_list.remove('goose')
#this deletes the element with the value taken in

print(my_list)
['duck', 'buck']
Enter fullscreen mode Exit fullscreen mode

The remove() function takes a value and deletes the first occurence of the element with the matching value.

my_list = ['duck', 'buck', 'goose']
#this creates a list of 3 elements

my_list.clear()
#empties the list

print(my_list)
[]
Enter fullscreen mode Exit fullscreen mode

The clear() function does not take any parameters, removes all elements from a list, and does not return anything.

Accessing Elements

Accessing elements in a list can be done by passing in the index values.

my_list = ['duck', 'buck', 'goose']
#this creates a list of 3 elements

print(my_list[2])
#access element at index 2
'goose'
Enter fullscreen mode Exit fullscreen mode

Here, print(my_list[2]), is accessing the element that is at index 2 which in this case would be 'goose'.

Dictionary

A dictionary in Python is used to store key-value pairs, where if the key is accessed, the values associated are also obtained. To create a dictionary in Python, the flower braces can be used or the dict() function. The key-value pairs must be added when working with dictionaries.

my_dict = {}
#creates an empty dictionary

my_dict = {'Mario' : 'Red', 'Luigi' : 'Green'}
#creates a dictionary with two key-value pairs

print(my_dict)
{'Mario':'Red','Luigi':'Green'}
Enter fullscreen mode Exit fullscreen mode

Here my_dict has two key-value pairs. The first key-value pair has a key 'Mario' with the associated value of 'Red' whereas the second key 'Luigi' has the associated value of 'Green'.

Changing and Adding Key-Value pairs in Dictionaries

In the case of when dealing with dictionaries, changing the values can be done by acessing the keys. In other words, you must first access the key of a pair before you can change the value.

my_dict = {'Mario' : 'Red', 'Luigi' : 'Green'}
#creates a dictionary with two key-value pairs

my_dict['Luigi'] = 'Purple'
#access the pair with the matching key, then updates the value associated with it

print(my_dict)
{'Mario':'Red','Luigi':'Purple'}

Enter fullscreen mode Exit fullscreen mode

Here, the key'Luigi' is being passed into my_dict, accessing the key-value pair with the matching key and then changing the value from 'Green' to 'Purple'.

my_dict = {'Mario' : 'Red', 'Luigi' : 'Green'}
#creates a dictionary with two key-value pairs

my_dict['Wario'] = 'Yellow'
#creates a new key-value pair 

print(my_dict)
{'Mario':'Red', 'Luigi':'Green','Wario':'Yellow'}

Enter fullscreen mode Exit fullscreen mode

Here, the key Wario is being passed into my_dict and because there is no existing key that matches it, will create one and then associate the value Yellow to it. The new key-value pair gets added to my_dict and now the dictionary has 3 key-value pairs.

Deleting Key-Value Pairs

Similarly to lists, to delete key-value pairs, the pop() and clear() function can be used.

my_dict = {'Mario' : 'Red', 'Luigi' : 'Green'}
#creates a dictionary with two key-value pairs

v = my_dict.pop('Luigi')
#pops the value with the associated key 'Luigi' and gives it back

print('Value: ', v)
print('Dictionary: ', my_dict)

Value: Green
Dictonary: {'Mario': 'Red'}

Enter fullscreen mode Exit fullscreen mode

Here, the value is being returned by accessing the key of Luigi in my_dict.

my_dict = {'Mario' : 'Red', 'Luigi' : 'Green'}
#creates a dictionary with two key-value pairs

my_dict.clear()
#empties the dictionary

print(my_dict)
{}
Enter fullscreen mode Exit fullscreen mode

Here, the clear() function does not take any parameters, deletes all key-pair values from the dictonary, and does not return anything.

Tuple

Tuples are the same as lists but have the special rule that once the data has entered in the tuple, it cannot be changed no matter the circumstance. However, the exception to that rule is that if the data inside the tuple is mutable, then the tuple data can be changed. To create a tuple, parenthesis are used or the tuple() function.

my_tuple = (1, 7, 9, 4)
#creates a tuple with 4 elements

print(my_tuple)
(1, 7, 9, 4)
Enter fullscreen mode Exit fullscreen mode
my_tuple = (1, 7, 9, 4)
#creates a tuple with 4 elements

my_tuple = my_tuple + (0, 2)
#adds elements to the tuple

print(my_tuple)
(1, 7, 9, 4, 0, 2)
Enter fullscreen mode Exit fullscreen mode

To append elements to an existing tuple, the '+' operator can take another tuple to be appended to it.

Sets

Sets are a colleciton of unordered elements that are unique, meaning that the elements can be repeated more than one time but will enter the set only once. Sets are created using flower braces where values are only passed into it.

my_set = {2, 2, 2, 3, 3, 4, 5, 10}
#this creates a set

print(my_set)
{2, 3, 4, 5, 10}
Enter fullscreen mode Exit fullscreen mode

Here, despite that my_set has repeating elements, my_set is printed with 5 elements only.

Adding Elements to Sets

To add elements to sets, a value is passed to the add() function.

my_set = {2, 2, 2, 3, 3, 4, 5, 10}
#this creates a set

my_set.add(9)
#adds an element to the set

print(my_set)
{2, 3, 4, 5, 10, 9}
Enter fullscreen mode Exit fullscreen mode

Here, the add() function added a unique element to the set, and since it is unique, is entered to the set.

Conclusion

Now having been introduced to built-in data structures in Python, create a space for yourself to work with data you are familiar with and get comfortable with manipulating data. Organizing, managing and storing data are a collection of skills that can be cultivated through trial and effort in a programmer's career. It is neccessary to have a solid understanding and handling of data, since data is what ultimately powers AI and advanced data applications. Remember to consistently check yourself with what you are doing with your code, and why you are writing your code. Knowledge is power, and with new-found knowledge, you can dispel your doubts and take one step closer to paving your path into the world of software engineering!

Resources

https://www.edureka.co/blog/data-structures-in-python/#:~:text=Organizing%2C%20managing%20and%20storing%20data,perform%20operations%20on%20them%20accordingly.

https://dev.to/codewithshan/13-reasons-to-learn-data-structures-and-algorithms-2022-bbl#:~:text=Data%20structures%20and%20algorithms%20are%20the%20building%20blocks%20of%20large,you%20smarter%20as%20a%20programmer.

https://www.indeed.com/career-advice/career-development/career-goals-software-engineer#:~:text=Software%20engineers%20design%20and%20develop,are%20skilled%20in%20both%20areas.

Top comments (0)