Data Structures

2.2. Data Structures#

Welcome to an exciting new adventure in data management! In the previous module, you learned how to create variables, which was just the tip of the iceberg. Now, imagine a scenario where you have numerous variables or want to organize and access them within a single entity. That’s where data structures come into play!

Data structures are like superheroes that help us tackle complex data management challenges. They come in various forms, each with its unique purpose and complexity. Today, we’ll dive into some of the superheroes of Python’s built-in data structures: the mighty list, the versatile dict, and the steadfast tuple. These data structures provide us with powerful tools to store, organize, and manipulate data, enabling us to unleash the true potential of our code. Let’s dive in and unlock the secrets of organized data!

2.2.1. List#

A list is a powerful data structure in Python that allows you to store and manipulate an ordered collection of items. It’s like having a magic box where you can keep multiple things together and change them whenever you want. Let’s explore 4 different ways to create and use lists:

  1. Creating an empty list: You can create an empty list by simply using empty square brackets [ ]. It’s like having a blank canvas ready to be filled with items.

my_list = [2,5,3]

print (my_list)
[2, 5, 3]
  1. Creating an empty list - using the class constructor: You can also use the list class constructor list() to create an empty list. It’s another way of preparing your container for future data.

my_list = list()
  1. Creating a list from existing data: To create a list with existing data, you can directly enclose the items within square brackets [ ], separating them with commas. It’s like assembling your collection of items in one go.

fruits = ['apple', 'banana', 'orange', 'kiwi']
print (fruits)
['apple', 'banana', 'orange', 'kiwi']
  1. Creating a list from existing data: You can use the list constructor list( ) with an iterable (such as a string or another list) to create a new list containing the elements of that iterable. It’s like transforming one collection into another.

numbers = list(range(1, 6))  # Creates a list [1, 2, 3, 4, 5]
print (numbers)
[1, 2, 3, 4, 5]

2.2.2. tuple#

A tuple is a data structure in Python that allows you to store an ordered collection of items. Unlike lists, tuples are immutable, meaning their elements cannot be modified once they are assigned. Let’s explore how to define tuples and create examples:

  1. Defining a tuple using ( ) brackets or comma: You can define a tuple by enclosing the items within parentheses () or by simply separating them with commas. It’s like creating a fixed ensemble of elements.

my_tuple1 = (1, 2, 3)
my_tuple2 = 1, 2, 3

print (my_tuple1)
print (my_tuple2)
(1, 2, 3)
(1, 2, 3)
  1. Creating a tuple using the tuple class constructor: You can also use the tuple class constructor tuple() to create a tuple. It accepts an iterable as an argument and generates a tuple with its elements. It’s like assembling data into a coordinated ensemble.

my_tuple3 = tuple([4, 5, 6])

coordinates = (3, 5)  
days_of_week = tuple(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])  

my_tuple4 = (1, 2, 3)
print (my_tuple3)
print (my_tuple4)
print (coordinates)
print (days_of_week)
(4, 5, 6)
(1, 2, 3)
(3, 5)
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')

2.2.3. Dict#

A dictionary is a versatile data structure in Python that allows you to store and retrieve data using a key-value pairing. It’s like having a real-life dictionary where you can quickly look up information using specific words. Let’s explore how to define dictionaries, we will explore 3 examples:

  1. Creating a dictionary using { } brackets: You can create a dictionary by enclosing key-value pairs within curly braces { }, separating each pair with a colon ‘ : ‘ It’s like building a repository of information with quick access to specific entries.

my_dict = {"name": "John", "age": 25, "city": "New York"}

print (my_dict)
{'name': 'John', 'age': 25, 'city': 'New York'}
  1. Creating an empty dictionary using the class constructor: You can also use the dict class constructor dict( ) to create an empty dictionary. It’s like preparing a blank canvas to populate with key-value pairs later.

my_dict2 = dict()
  1. Creating a non-empty dictionary by specifying pairs of key-value patterns: To create a dictionary with existing data, you can specify pairs of key-value patterns within the curly braces { }. Each key-value pair is separated by a colon : and pairs are separated by commas. It’s like defining relationships between different pieces of information.

student_scores = {"Alice": 85, "Bob": 92, "Charlie": 78}
contacts = {"John": "+123456789", "Emily": "+987654321", "Sam": "+345678912"}
my_dict3 = dict()
my_dict3["name"] = "Alice"
my_dict3["age"] = 30
my_dict3["city"] = "London"

print (my_dict3)
{'name': 'Alice', 'age': 30, 'city': 'London'}

In the above code cell, we have created two dictionaries: student_scores and contacts, by specifying key-value pairs within the { } brackets. Additionally, we have also demonstrated how to create an empty dictionary my_dict3 using the dict( ) constructor and then added key-value pairs to it using the square bracket notation. These examples showcase the flexibility of dictionaries in storing and accessing data through key-value relationships.