sec02 - Data Structures [Overview]
スポンサーリンク

What is data structure?

Python provides data structures that allow a single variable to hold multiple pieces of data. For example, the list data structure can hold data as follows.

num_list = [1, 2, 3]

Lists can manage multiple pieces of data in order, starting from element number [0]. The above code is a simple example, but it allows you to manage the three pieces of data 1, 2, and 3 in the variable “num_list.”

In the ‘Data Structures’ section, we will delve deeper into the data structures available in Python, starting with lists.

What you will learn in this section

In this section, you will learn the following data structures.

  1. list
  2. tuple
  3. dict
  4. set

For each of the above data structures, you will learn about “overview,” “slicing,” “editing,” “methods,” and “copying.”

Details will be explained in each lecture, but this page introduces the features of each.

スポンサーリンク

list

As mentioned earlier, a list is a data structure that manages multiple data items in order of element number. A list defines each element within square brackets ([]).

num_list = [1, 2, 3]

The features of a list are as follows.

  • Can store multiple values in order
  • Elements can be added, changed, or deleted later
  • Advantages: High flexibility
  • Disadvantages: Operations become slow when there are many elements

tuple

A tuple is a data structure that manages multiple data items in order, similar to a list, but it is a data structure that cannot add, change, or delete elements. A tuple defines each element within parentheses (()).

num_tuple = (1, 2, 3)

The features of a tuple are as follows.

  • Saves values in order and cannot be changed after creation
    • (In Python, data that cannot be changed after creation is called an “immutable object.”)
  • Advantages: High security and fast processing
  • Disadvantages: Values cannot be changed

dict

dict (dictionary) is a data structure that manages keys and values linked together. Data in dict is defined in the form {key: value}.

# Manage countries and capitals in a dict
capitals = {
    'Japan': 'Tokyo',
    'United States': 'Washington, D.C.',
    'France': 'Paris',
    'Brazil': 'Brasilia'
}
# Get the capital
capital_japan = capitals['Japan']
capital_france = capitals['France']

print(capital_japan) # Tokyo
print(capital_france) # Paris

The features of a dict are as follows.

  • Store key and value as pairs and quickly search for values from keys
  • Advantages: Fast searching
  • Disadvantages: Key management is required

set

set is a structure that collects non-duplicate values in no particular order.

Just like the sets you learn in math class, you can determine whether a piece of data is an element of a set, and you can also investigate union, intersection, difference, symmetric difference, subsets, supersets, prime sets, and other concepts.

set defines each element within curly brackets ({}).

num_set = {1, 2, 3}

The features of a set are as follows.

  • A structure that collects unique values in no particular order
  • Advantages: Fast duplicate removal and set operations
  • Disadvantages: Element order is not guaranteed
  • Uses: Duplicate checking and set calculations

スポンサーリンク