📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
Edit on GitHub
  1. 10 - Sets and dictionaries

Which data structure should I use?

PreviousDictionariesNextDebugging

Last updated 1 year ago

In Python 3, lists, tuples, sets and dictionaries are all built-in collections. They differ in their properties and use cases.

  • List: A list is an ordered collection of elements that can contain duplicate values. It is mutable which means you can change its content.

  • Tuple: A tuple is similar to a list but it is immutable which means you cannot change its content once created .

  • Set: A set is an unordered collection of unique elements .

  • Dictionary: A dictionary stores key-value pairs. .

In Python, an ordered collection means that the elements in the collection have a specific order that is determined by their position in the collection. The order of elements is preserved when you add or remove elements from the collection. For example, a list is an ordered collection. If you create a list with the elements ['apple', 'banana', 'cherry'], the element 'apple' will always be before 'banana', and 'banana' will always be before 'cherry' . In other word the data structure retains the order in which the data was added.

On the other hand, a set is an unordered collection. If you create a set with the same elements as above ({'apple', 'banana', 'cherry'}), there is no guarantee about the order of elements when you iterate over them. The order in which they are added may not be retained by the data structure.

Collection
Mutable
Ordered
Indexing
Duplicate Data

List

✔

✔

✔

✔

Tuple

𐄂

✔

✔

✔

Set

✔

𐄂

𐄂

𐄂

Dictionary

✔

✔

✔

𐄂

The choice of which collection to use depends on the specific use case and requirements.

  • List: Use a list when you need an ordered collection of elements that can contain duplicates. Lists are useful for storing and retrieving elements based on their position in the collection .

  • Tuple: Use a tuple when you have an ordered collection of elements that should not be changed. Tuples are often used to represent a single record or data structure with multiple fields .

  • Set: Use a set when you need to store unique elements and perform set operations such as union, intersection and difference .

  • Dictionary: Use a dictionary when you need to store key-value pairs and retrieve values based on their keys. Dictionaries are useful for representing mappings from one set of values to another .

Here are some steps you can follow to determine which collection to use:

  1. Identify the requirements: Determine what kind of data you need to store and how you will be using it. Do you need to store ordered data? Do you need to store unique elements? Do you need to store key-value pairs?

  2. Evaluate the properties of each collection: Consider the properties of each collection and how they align with your requirements. For example, if you need to store ordered data that can contain duplicates, a list might be a good choice.

  3. Consider performance: Different collections have different performance characteristics for common operations such as adding, removing and accessing elements. Choose a collection that provides good performance for the operations you will be performing most frequently.

1
1
It is an insertion ordered collection since Python 3.7
2