Collections Data
Types
Types of Collections Data Types
• Tuples
• Lists
• Sets
• Dictionaries
• Iterating and copying collections
Tuples
• A tuple is a collection data type in Python.
• Similar to lists, tuples can store multiple items.
• Unlike lists, tuples are immutable, meaning their
contents cannot be changed once defined.
Creating Tuples
• Tuples are defined using parentheses ‘()’
• You can create an empty tuple or initialize it with values.
Accessing Elements:
• Elements in a tuple are accessed by their index, just like in lists
(0-based indexing).
Tuple Packing and Unpacking
• Tuple packing is when you group multiple values into a single
tuple.
• Tuple unpacking is when you assign the values of a tuple to
individual variables.
Immutability
• Once a tuple is created, you cannot modify its elements.
• You can, however, create a new tuple with modifications.
Common Operations
• You can use various operators on tuples, like concatenation (‘+’)
and repetition (‘*’)
Iterating Through Tuples
• You can iterate through a tuple using a ‘for’ loop
Use Cases for Tuples:
• Tuples are often used for situations where you want to store a fixed
collection of items that should not be changed.
• They can be used as keys in dictionaries due to their immutability.
When to Use Tuples vs. Lists:
• Use tuples when the data should not change (e.g., coordinates).
• Use lists when you need to modify the data (e.g., a list of tasks).
Tuple Methods:
• Tuples have a limited number of methods compared to lists because of
their immutability.
Lists
• A list is a collection data type in Python.
• Lists are used to store multiple items in a single
variable.
• Lists are ordered, mutable, and can contain
elements of different data types.
Creating Lists
• Lists are defined using square brackets ‘[]’
• You can create an empty list or initialize it with values.
Accessing Elements
Elements in a list are accessed by their index, starting from 0
(0-based indexing).
Modifying Lists
Lists are mutable, which means you can change their
contents.
List Slicing
• You can extract a portion of a list using slicing.
• Slicing uses a colon ‘:’ to specify a range of indices.
Common List Operations
Lists support operations like concatenation (‘+’) and
repetition (‘*’)
Iterating Through Lists
You can iterate through a list using a ‘for’ loop.
List Comprehensions
List comprehensions provide a concise way to create lists
based on existing lists.
Use Cases for Lists:
• Lists are versatile and used for various purposes.
• They are suitable for collections of items where the order and
modification of data are important.
List Methods:
• Lists have many built-in methods for common operations like
sorting, reversing, and more.
Sets
• A set is a collection data type in Python.
• Sets are used to store multiple unique items in a
single variable.
• Sets are unordered and mutable, but their
elements are unique.
Creating Sets
• Sets are defined using curly braces ‘{}’ or the ‘set()’
constructor.
• You can create an empty set or initialize it with values.
Adding and Removing
Elements
• You can add elements to a set using the ‘add()’ method.
• You can remove elements using the ‘remove()’ method.
Set Operations
Sets support various operations such as union (‘|’),
intersection (‘&’), and difference (‘-’)
Iterating Through Sets
You can iterate through a set using a ‘for’ loop.
Use Cases for Sets:
• Sets are used when you need to store a collection of unique items.
• They are useful for removing duplicate values from a list or
checking for membership.
Frozensets:
• A frozenset is an immutable version of a set.
• Frozensets can be used as dictionary keys because they are
hashable.
Set Methods
Sets have various built-in methods for common operations like
clearing, copying, and checking for membership.
Set Comprehensions
Set comprehensions provide a concise way to create sets
based on existing iterables.
Dictionaries
• A dictionary is a collection data type in Python.
• Dictionaries are used to store data in the form of key-
value pairs.
• Dictionaries are unordered, mutable, and each key
in a dictionary is unique.
Creating Dictionaries
Dictionaries are defined using curly braces ‘{}’ with key-
value pairs separated by colons ‘:’
Accessing Elements
Dictionary elements are accessed by their keys.
Modifying Dictionaries
Dictionaries are mutable, meaning you can change their
contents.
Common Dictionary Operations
Dictionaries support operations like copying, getting keys
and values, and checking for key existence.
Iterating Through Dictionaries
You can iterate through dictionaries using ‘for’ loops to access
keys, values, or key-value pairs.
Use Cases for Dictionaries:
• Dictionaries are used when you need to store and retrieve data
efficiently using meaningful keys.
• They are useful for tasks like mapping, configuration settings, and
data aggregation.
Nested Dictionaries:
• Dictionaries can contain other dictionaries as values, creating
nested data structures.
Iterating and
copying collections
Iterating Through Collections
Using ‘for’ Loops
• Python provides a simple way to iterate through collections
using ‘for’ loops.
Example - Iterating through a List:
Iterating Through Collections
Example - Iterating through a Set:
Example - Iterating through a Dictionary (Keys):
Iterating Through Collections
Example - Iterating through a Dictionary (Values)
Example - Iterating through a Dictionary (Key-Value Pairs):
Iterating Through Collections
Using List Comprehensions:
• List comprehensions are a concise way to create lists while
iterating through collections.
Example - List Comprehension:
Copying Collections
Shallow Copy:
• A shallow copy creates a new collection, but it still references the
same objects inside the collection (changes in nested objects affect
both).
Example - Shallow Copy of a List:
Example - Shallow Copy of a Dictionary:
Copying Collections
Deep Copy:
• A deep copy creates a completely independent copy of a collection,
including nested objects (changes don't affect the original).
Example - Deep Copy of a List:
Example - Deep Copy of a Dictionary: