The Wayback Machine - https://web.archive.org/web/20240707053028/https://www.geeksforgeeks.org/internal-implementation-of-data-structures-in-python/
Open In App

Internal implementation of Data Structures in Python

Last Updated : 08 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python provides a variety of built-in data structures, each with its own characteristics and internal implementations optimized for specific use cases.

In this article we are going to discuss about the most commonly used Data structures in Python and a brief overview of their internal implementations:

Data Structure

Internal Implementation

Static or Dynamic

Python List

Dynamic Arrays

Dynamic

Python Tuples

Arrays

Static

Python Sets

Hash Tables

Dynamic

Python Deque

Doubly linked list

Dynamic

Python Dictionary

Hash Tables

Dynamic

Python String

Array of Unicode characters

Dynamic

Python Array

Array

Static

Python HeapQ

Binary Trees

Dynamic

Python Hash maps

Hash Functions

Dynamic

Lists:

  • Lists are ordered collections of items, and their elements can be of different data types.
  • Internally, lists are implemented as dynamic arrays that can grow or shrink as needed. When the array is full, a new larger array is created, and elements are copied over.
  • This resizing operation is amortized, meaning it doesn’t happen every time an element is added.

Tuples:

  • Tuples are similar to lists but are immutable, meaning their elements cannot be changed after creation.
  • Internally, tuples are usually implemented as fixed-size arrays, and their immutability allows for some memory optimization and better performance in certain scenarios.

Sets:

  • Sets are unordered collections of unique elements.
  • Internally, sets are implemented using hash tables. Each element is hashed, and the hash value is used to quickly locate the element within the table.

Dictionaries:

  • Dictionaries (dicts) are collections of key-value pairs, where keys are unique and immutable.
  • Dicts are implemented using hash tables as well. Keys are hashed to determine their position in the table, and values are associated with these keys.

Strings:

  • Strings are sequences of characters.
  • Internally, Python strings are usually implemented as arrays of Unicode characters. The specific implementation details can vary depending on factors like Python version and compilation options.

Arrays:

  • Python provides an array module that allows you to create arrays with elements of a specific data type.
  • These arrays are more memory-efficient than lists when dealing with large quantities of homogeneous data.

Heap:

  • Python’s heapq module provides heap-related functions.
  • Heaps are binary trees that satisfy the heap property (parent nodes are smaller or larger than their children).

Collections from the collections module:

  • This module provides specialized data structures like defaultdict, Counter, OrderedDict, and deque.
  • They offer optimized implementations for specific use cases, such as default values, counting elements, ordered insertion, and double-ended queues.

Hash Maps:

  • Also known as hash tables or associative arrays.
  • Similar to dictionaries in Python, they store key-value pairs and offer fast access to values based on their keys.
  • Internally, they use a hash function to convert keys into indices in an array, allowing for efficient lookups.

Graphs:

  • Graphs consist of nodes (vertices) and edges connecting these nodes.
  • They can be implemented using various structures, such as adjacency matrices (2D arrays) or adjacency lists (lists of linked nodes).
  • Graphs are used to model relationships between entities.

Remember that the exact internal implementation details might vary between Python versions and implementations (like CPython, Jython, IronPython, etc.). The Python language abstracts these implementation details and provides a consistent interface for developers to work with these data structures.



Similar Reads

Internal Implementation of Linux Sort Command
Prerequisite: SORT command in Linux/Unix with examples The SORT command in the Linux operating system is used for sorting. It can be done either for files or for input on a command line given by the user. The sort command by default sorts files under the assumption that their data is ASCII. It sorts the files line by line. The SORT command follows
8 min read
Implementation of Chinese Remainder theorem (Inverse Modulo based implementation)
We are given two arrays num[0..k-1] and rem[0..k-1]. In num[0..k-1], every pair is coprime (gcd for every pair is 1). We need to find minimum positive number x such that: x % num[0] = rem[0], x % num[1] = rem[1], ....................... x % num[k-1] = rem[k-1] Example: Input: num[] = {3, 4, 5}, rem[] = {2, 3, 1} Output: 11 Explanation: 11 is the sm
11 min read
Does a Data Scientist/Machine Learning Engineer require in depth knowledge of Data Structures and Algorithms?
In today's world, data scientists and machine learning engineers play a crucial role in analyzing data and building intelligent systems. As technology continues to advance, the demand for these experts is growing rapidly. Real-world data problems are complex, requiring strong skills in handling data and creating efficient algorithms. In this articl
10 min read
Internal working of list in Python
Introduction to Python lists : Python lists are internally represented as arrays. The idea used is similar to implementation of vectors in C++ or ArrayList in Java. The costly operations are inserting and deleting items near the beginning (as everything has to be moved). Insert at the end also becomes costly if preallocated space becomes full.We ca
3 min read
Internal working of Python
Python is an object-oriented programming language like Java. Python is called an interpreted language. Python uses code modules that are interchangeable instead of a single long list of instructions that was standard for functional programming languages. The standard implementation of Python is called "cpython". It is the default and widely used im
5 min read
Internal Working of the len() Function in Python
The len() function in Python has a very peculiar characteristic that one had often wondered about. It takes absolutely no time, and equal time, in calculating the lengths of iterable data structures(string, array, tuple, etc.), irrespective of the size or type of data. This obviously implies O(1) time complexity. But have you wondered How? Python f
2 min read
Internal Structure of Python Dictionary
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized The dictionary consists of a number of buckets. Each of these buckets cont
3 min read
marshal — Internal Python object serialization
Serializing a data means converting it into a string of bytes and later reconstructing it from such a string. If the data is composed entirely of fundamental Python objects, the fastest way to serialize the data is by using marshal module (For user defined classes, Pickle should be preferred). Marshal module contains functions that can read and wri
2 min read
Internal working of Set in Python
Sets and their working Set in Python can be defined as the collection of items. In Python, these are basically used to include membership testing and eliminating duplicate entries. The data structure used in this is Hashing, a popular technique to perform insertion, deletion and traversal in O(1) on average. The operations on Hash Table are some wh
3 min read
Learn DSA with Python | Python Data Structures and Algorithms
This tutorial is a beginner-friendly guide for learning data structures and algorithms using Python. In this article, we will discuss the in-built data structures such as lists, tuples, dictionaries, etc, and some user-defined data structures such as linked lists, trees, graphs, etc, and traversal as well as searching and sorting algorithms with th
15+ min read
Article Tags :
Practice Tags :
three90RightbarBannerImg