Introduction
A data structure is a way of organizing and storing data so it can be accessed, updated, and processed efficiently. Python provides several built-in data structures, each designed for different types of data and use cases. The most commonly used data structures are:- Strings (
str) – Store textual data. - Lists (
list) – Ordered, mutable collections. - Tuples (
tuple) – Ordered, immutable collections. - Sets (
set) – Unordered collections of unique elements. - Dictionaries (
dict) – Store data as key-value pairs. - Deque (
collections.deque) – A double-ended queue that supports fast insertion and deletion from both ends.
Overview
Strings
A string (str) is an immutable sequence of Unicode characters used to store text.
Creating Strings
Strings can be created using single quotes, double quotes, or triple quotes.Indexing
Access individual characters using their position.Slicing
Extract a portion of a string usingstart:stop:step.
String Operators
Common String Methods
Splitting and Joining Strings
Usesplit() to convert a string into a list and join() to combine a list into a string.
String Formatting
Use f-strings to insert variables into a string.Strings are Immutable
Strings cannot be modified after creation.Python Nuances
- Strings are immutable.
- Strings support indexing and slicing.
- Negative indexing starts from the end.
- Use
split()to convert a string into a list. - Use
join()to combine a list into a string. - Prefer f-strings for string formatting.
Lists
A list is an ordered, mutable collection that can store elements of different data types. Lists are one of the most commonly used data structures in Python.Creating Lists
Accessing Elements
Lists support indexing and slicing.Common List Methods
List Comprehension
List comprehensions provide a concise way to create lists.Python Nuances
- Lists are ordered and mutable.
- Lists can store elements of different data types.
- Lists support indexing and slicing.
- Duplicate values are allowed.
- Use list comprehensions for concise list creation.
Tuples
A tuple is an ordered, immutable collection that can store elements of different data types. Once created, a tuple cannot be modified.Creating Tuples
Note: A single-element tuple must include a trailing comma.
Accessing Elements
Tuples support indexing and slicing.Tuple Packing and Unpacking
Python Nuances
- Tuples are ordered and immutable.
- Tuples can store elements of different data types.
- Tuples support indexing and slicing.
- Duplicate values are allowed.
- Tuples are commonly used for fixed data that should not change.
- Remember the trailing comma when creating a single-element tuple.
Sets
A set is an unordered, mutable collection of unique elements. Sets automatically remove duplicate values and are useful for performing mathematical set operations.Creating Sets
Note: Useset()to create an empty set. Using{}creates an empty dictionary.
Common Set Methods
Set Operations
Python Nuances
- Sets store unique elements only.
- Sets are unordered, so indexing and slicing are not supported.
- Duplicate values are automatically removed.
- Sets are useful for membership testing and removing duplicates.
- Use
set()to create an empty set.
Dictionaries
A dictionary (dict) stores data as key-value pairs. Keys are unique and are used to access their corresponding values.
Creating Dictionaries
Accessing Values
Access values using their keys.Note:get()returnsNone(or a default value) if the key does not exist, instead of raising an error.
Adding and Updating
Removing Items
Common Dictionary Methods
Iterating Through a Dictionary
Dictionary Comprehension
Python Nuances
- Dictionaries store data as key-value pairs.
- Keys must be unique and immutable.
- Values can be of any data type.
- Dictionaries are mutable.
- Dictionaries preserve insertion order (Python 3.7+).
- Use
get()when a key may not exist.
deque
A deque (double-ended queue) is a list-like data structure from Python’s built-incollections module. It allows fast insertion and deletion of elements from both the beginning and the end, making it more efficient than a list for queue-like operations.
Creating a deque
Common Operations
Rotating a deque
Python Nuances
dequestands for double-ended queue.- Supports fast insertion and deletion from both ends.
- Maintains the order of elements.
- Allows duplicate values.
- Ideal for implementing queues, stacks, and sliding window algorithms.
- Prefer
dequeoverlistwhen frequently adding or removing elements from the beginning.