Introduction to Python Data Structures

Introduction to Python Data Structures

Here, we’ll learn about data structures as they relate to the Python programming language.

Overview of Data Structures

Data structures are fundamental concepts in computer science that help write efficient programs in any language. Python is a high-level, interpreted, interactive, and object-oriented scripting language that allows us to explore the fundamentals of data structures in a simpler way than with other programming languages.

In this chapter, we’ll provide a brief overview of some common data structures and how they relate to specific Python data types. There are also some Python-specific data structures, listed in another category.

General Data Structures

The various data structures used in computer science can be broadly categorized into the following two categories. We’ll discuss each of these data structures in detail in the following chapters.

Linear Data Structures

These are data structures that store data elements in a sequential manner.

  • Array – It is a data structure that is arranged in a sequence, paired with the index of the data element.
  • Linked List – Each data element contains a link to another element, as well as the data present in it.

  • Stack – It is a data structure that follows a specific order of operations, either LIFO (Last In, First Out) or FILO (First In, Last Out).

  • Queue – It is similar to a stack, but the order of operations is FIFO (First In, First Out).

  • Matrix – It is a two-dimensional data structure in which data elements are referred to by a pair of indices.

Non-Linear Data Structures

These are data structures in which there is no sequential linking of the data elements. Any pair or group of data elements can be linked to each other and accessed without a strict order.

  • Binary Tree – This is a data structure in which each data element can be linked to at most two other data elements, starting from a root node.
  • Heap – This is a special case of a tree data structure where the data in a parent node is either strictly greater than/equal to, or strictly less than, its child nodes.

  • Hash Table – This is a data structure composed of linked arrays using a hash function. It uses keys rather than indices to retrieve the values of data elements.

  • Graph – This is an arrangement of vertices and nodes, some of which are connected by links.

Python-Specific Data Structures

These data structures are specific to the Python language and provide greater flexibility in storing different types of data and are faster to work with in the Python environment.

  • Lists – These are similar to arrays, except that the data elements can be of different data types. In a Python list, you can have both numbers and strings.
  • Tuples – Tuples are similar to lists, but they are immutable, meaning that the values in a tuple cannot be modified, only read.

  • Dictionaries – Dictionaries contain key-value pairs as their data elements.

In the following chapters, we will learn the details of how these data structures are implemented in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *