Understanding Python Compound Data Types: A Comprehensive Guide

Promethee Spathis
4 min readJul 23, 2024

In this article, I present a Python aide-mémoire that will help you understand the various compound data types offered in Python.

Understanding and using the compound data types effectively can greatly improve the efficiency and organization of your Python code.

Download the Aide-Mémoire

This comprehensive aide-mémoire is completely free and easily accessible online. It covers the main compound data types of Python 3 with examples.

To improve the efficiency and organization of your Python code, do yourself a favor and utilize this cheat sheet. Download the cheat sheet here.

Preview of the Python Compound Data Types Cheat Sheet

What is a Compound Data Type?

Compound data types in Python are types that can hold multiple items or values. They are used to group together multiple values and can be of different data types.

my_list = [1, 1, 2, 'A', 'B', 'C']
my_tuple = (1, 1, 2, 'A', 'B', 'C')
my_set = {1, 2, 'A', 'B', 'C'}
my_dictionary = {1:'A', 2:'B', 3:'C'}
my_string = '112ABC'
my_bytes = b'112ABC'
my_bytearray = bytearray(b'112ABC')

Compound data types provide various ways to group and manipulate multiple items in Python, allowing for efficient data management and operations.

What are the Compound Data Types in Python?

In Python, the main compound types are:

  • List
  • Tuple
  • Set
  • Dictionary
  • String
  • Bytes
  • Bytearray

Similarities and Differences among Python Compound Data Types

In Python, compound data types share some similarities but also exhibit certain differences. Understanding these can help you choose the right type for your needs.

Similarities

  1. Iterable: All compound data types are iterable, meaning you can loop over their elements.
  2. Contain Multiple Elements: They can hold multiple items or elements.
  3. Support Membership Testing: You can use the in and not in keywords to check for the presence of an element.
  4. Indexable: Lists, tuples, strings, bytes, and bytearrays support indexing, allowing you to access elements by their position.
  5. Support for Common Operations: You can perform operations like len() to get the number of elements.

Differences

1. Mutability: Mutability refers to the ability of an object to change its state or contents after it has been created. Immutable data types make better use of memory through memory sharing, optimization, elimination of defensive copies, and simpler memory management.

  • Mutable: Lists, sets, dictionaries, bytearrays.
  • Immutable: Tuples, strings, bytes.

2. Order: The order in which the elements are specified when the object is defined is maintained for the object’s lifetime unless explicitly changed.

  • Ordered: Lists, tuples, strings, bytes, bytearrays.
  • Unordered: Sets, dictionaries (though dictionaries maintain insertion order since Python 3.7).

3. Uniqueness: Some compound data types allow for duplicate elements to be added.

  • Unique Elements: Sets and dictionary keys.
  • Duplicated Elements: Lists, tuples, strings, bytes, bytearrays.

4. Indexing and Slicing: Indexing refers to the process of accessing a specific element in a sequence using its position or index number. Slicing refers to the method of extracting a portion of a sequence.

  • Supports Indexing and Slicing: Lists, tuples, strings, bytes, bytearrays.
  • Does Not Support Indexing and Slicing: Sets, dictionaries (access elements via keys in dictionaries).

5. Access times vs memory usage:

  • Sets and dictionaries are best for fast searches due to their underlying hash table implementation. Highly efficient lookups come at the cost of higher memory usage due to the metada required to manage the hash table structure.
  • Tuples and strings are more memory-efficient due to their immutability and fixed size.
  • Lists are less efficient than tuples because they are mutable and may require extra space for dynamic resizing and additional memory management overhead.

Summary of Compound Data Types

The similarities and differences among Python Compound Data Types are summarized in the following table:

Similarities and differences among Python Compound Data Types

About the Author

Promethee Spathis is a Professor of Practice in Computer Science at NYU Shanghai. He is also a Senior Associate Professor in the Faculty of Science & Engineering at Sorbonne Université, France. He received both his Masters and PhD degree in Computer Science from Sorbonne Université.

With a primary focus on Computer Science, Professor Spathis has designed and developed a wide array of courses spanning B.Sc, M.Sc, and Ph.D. levels. At NYU Shanghai, he currently teaches courses in computer networking and computer programming, bringing his extensive expertise and innovative teaching methods to the classroom. His commitment to education and research continues to inspire and shape the next generation of computer scientists.

Keywords

Python Cheat Sheet, Python Quick Reference, Python Tips and Tricks, Python for Beginners, Python Programming Guide, Python Syntax Cheat Sheet, Python Coding Shortcuts, Python Basics, Python Code Snippets, Python Essential Commands, Learn Python Quickly, Python One-liners, Python Reference Guide, Python Programming Basics, Python Developer Tools, Python Quick Tips, Python Code Examples

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response