Types of Data Structures in Python

Posted by Marta on December 18, 2020 Viewed 3623 times

Card image cap

I will show you the four types of data structures in python which are more commonly used.
The types of data structures are important when your programs get more complex and need to handle larger amount of data.

Types of Data Structures in Python https://img.youtube.com/vi/rKbIWDkFWk8/default.jpg Types of Data Structures in Python 2020-07-22 https://youtu.be/rKbIWDkFWk8

In python, there are basic data types like booleans, integers, floats and strings. You could think of these as atoms, then data structures are the molecules. We combine those basic pieces to create more complex data types.

Lists and Tuples

In python, you can represent a sequence of items with two structures:
list and tuples. Both are zero based. Why are there two structures? The list is a changeable structure, meaning you can add, remove, modify, after creation. A tuple is an unchangeable or immutable data structure. It can’t be modified once you created it. Let’s see what operations python offers to work with these data structures.

# Create a list
list = {'dog', 'cat', 'bear'}

#Access an item
list[0]

#Add item to the list
list.append('fish')

#Remove from the list 
list.remove('cat')
#or
del(list[1])

#Number of elements in the list
len(list)

#Iterate 
for animal in list:
  print(animal)

These are the most popular methods that you will all the times. Let’s move on to tuples.

A few more list operations

Create an empty list

empty_list = []

Reverse a list

reversed_list = list[::-1]

Tuples are just the same as a list, but unchangeable, or immutable. That means when you try to assign elements, to a tuple, after creation, you will get an error. Let’s see the operations python supports, when you are working with tuples.

#Create a tuple
tuple = ('dog','cat')

#Create a single item tuple
tuple_one = ('dog'),

#Access an item
tuple[0]

#Delete the tuple
del(tuple)

#Assign multiple values at once (TUPLE UNPACKING)
animal_one, animal_two = tuple


These are the most popular methods. An interesting thing about tuples. If you want to create a tuple with, just one value, you should add a comma.
This is called, trailing comma. See the example above 🙂

Dictionaries

Now we are gonna talk about dictionaries. A dictionary, another of the types of data structures in python, is a data collection, that store information, by key. In most cases, dictionaries are more efficient than a list. A dictionary is an, unordered collection, meaning, elements are accessed by their key, not by index. Dictionaries work, quite similar to a real life dictionary.The words, will be the keys, and the definitions, will be the value saved.

Another thing to know about dictionaries. They are changeable collections.

#Create a dictionary
city_dictionary = {
	'capital': 'London',
   	'country': 'UK'
}

#Access an item in the dictionary
city_dictionary['capital']

#Add an item to the dictionary
city_dictionary['language']='English'

#Remove an item
del(city_dictionary['language'])
# Or
city_dictionary.pop('language')

#Number of elements in the dictionary
len(city_dictionary)

#Iterate over the values in dictionary
for value in city_dictionary.values():
  print(value)
  
#Iterate over the keys
for key in city_dictionary.keys():
  print(key)

#Over both keys and values
for key, value in city_dictionary.items():
  print(key)
  print(value)

Something to watch out about dictionaries. They don’t allow duplicates, so if the new key that doesn’t exist in the dictionary, it will be added. However, if it already exists ,It will replace it. And finally the last data structure!

Sets

A set is like a dictionary, without the values. You used a set, when you want to know, if something exists. But nothing else, you can’t save extra information. It’s an unordered collection, like the dictionary. Changeable. And it doesn’t allow, duplicates. Let’s see the operation that python supports to work with sets.

#Create a set
set = {'London', 'Dublin'}

#Check if something exists in the set
'London' in set

#Add an element to the set
set.add('Madrid')

#Remove an element
set.remove('Madrid')

#Iterate over the set
for set_item in set:
  print(set_item)

These are the most common structure. Something interesting to know is that you can also combine these data structures, and create more complex data types. You could create a list of dictionaries. For example, a city list, each city being, a dictionary. Or a list of lists. You can combine these structures, in any way you like.

# List of dictionaries
#One dictionary
city1 = {
  'name':'Madrid',
  'country': 'Spain'
}
#Another dictionary
city2 = {
  'name':'London',
  'country':'UK'
}

#List of dictionaries
cities = [city1, city2]

What’s next?

Put all you learn in practice working on these projects: Four Projects for beginners

Project-Based Programming Introduction

Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications

100% Recommended book for Java Beginners

Unique lay-out and teaching programming style helping new concepts stick in your memory

90 Specific Ways to Write Better Python

Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples

Grow Your Java skills as a developer

Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.

Write Code as a Professional Developer

Excellent read for anyone who already know how to program and want to learn Best Practices

Every Developer should read this

Perfect book for anyone transitioning into the mid/mid-senior developer level

Great preparation for interviews

Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java