Chapter 2 Collections
list: Ordered collection of items, e.g.,my_list = [1, 2, 3].tuple: Immutable ordered collection of items, e.g.,my_tuple = (1, 2, 3).set: Unordered collection of unique items, e.g.,my_set = {1, 2, 3}.dict: Key-value pairs, e.g.,my_dict = {'a': 1, 'b': 2}.
2.0.1 Properties
In Python, collections are built-in data types that can be used to group multiple elements together. Here are some common properties of Python collections:
2.0.1.1 Mutable vs. Immutable:
Mutable Collections: Lists (
list), Sets (set), and Dictionaries (dict) are mutable. You can modify their contents after creation.Immutable Collections: Tuples (
tuple) and Strings (str) are immutable. Once created, their contents cannot be changed.
2.0.1.2 Ordering:
Ordered Collections: Lists and Tuples maintain the order of elements. Elements are stored in the order they were added.
Unordered Collections: Sets and Dictionaries do not guarantee any specific order. The order of elements may not be the same as the order of insertion.
2.0.1.3 Indexing and Slicing:
- Indexing: Lists, Tuples, and Strings support indexing. Elements can be accessed using indices (0-based).
- Slicing: Lists, Tuples, and Strings support slicing to create sub-collections.
2.0.1.4 Uniqueness:
- Unique Elements: Sets only contain unique elements. If you try to add an element that already exists, it won’t be added again.
2.0.1.5 Key-Value Pairs (Dictionaries):
- Associative Data: Dictionaries consist of key-value pairs, allowing you to associate values with unique keys.
2.0.1.6 Heterogeneity:
- Mixed Types: Lists, Tuples, and Sets can contain elements of different data types.
2.0.1.9 Using add() for set objects:
## {1, 2, 3, 100}
- Removing Elements:
remove(),pop(),discard(),clear()
2.0.1.10 remove() Method
Purpose: Removes the first occurrence of a specified value from a list or set.
Usage:
For lists, if the specified value does not exist, it raises a ValueError.
For sets, it also raises a KeyError if the specified value is not present.
## [1, 3, 2, 4]
2.0.1.11 pop() Method
Purpose: Removes and returns an element from a list or set.
Usage:
For lists, it removes and returns the element at a specified index. If no index is provided, it removes and returns the last element. Raises an IndexError if the list is empty.
For sets, it removes and returns an arbitrary element because sets are unordered. Raises a KeyError if the set is empty.
my_list = [1, 2, 3, 4]
# Removes and returns the last element by default
last_element = my_list.pop()
print(last_element) # Output: 4## 4
## [1, 2, 3]
# Removes and returns the element at index 1
second_element = my_list.pop(1)
print(second_element) # Output: 2## 2
## [1, 3]
2.0.1.12 discard() Method
Purpose: Removes the specified element from a set.
Usage: Sets only: Does not raise an error if the specified element does not exist, unlike remove().
## {1, 3}
2.0.1.13 clear() Method
Purpose: Removes all elements from a list or set, making it empty.
Usage: Can be used with both lists and sets to empty them.
## []
2.0.1.14 Extra: Imuutable
Yes, in Python, strings are immutable objects. This means that once a string is created, you cannot change its content. Any operation that appears to modify a string actually creates a new string. This immutability has several implications:
- No In-Place Modifications:
- You cannot modify a string directly by changing a character at a specific index, like you can with a list.
- Creating New Strings:
- Operations like concatenation or slicing create new strings rather than modifying the original.
- Hashing:
- Because strings are immutable, they can be used as keys in dictionaries and elements in sets. Their hash value remains constant.
- Memory Efficiency:
- Python can optimize memory usage by reusing the same string in memory if it already exists, thanks to immutability.
Understanding the immutability of strings is important when working with them in Python to avoid unexpected behavior and to write efficient and correct code. If you need to modify a string, you typically create a new string with the desired changes.
2.0.2 1. lists
A list is a sequenced collection of different objects such as integers, strings, and even other lists as well. The address of each element within a list is called an index. An index is used to access and refer to items within a list.
Lists can contain strings, floats, and integers. We can nest other lists, and we can also nest tuples and other data structures. The same indexing conventions apply for nesting:
list are like tuples, ordered sequences.
But lists are mutable.
A list is a built-in data type used to store an ordered collection of items. Lists are mutable, which means you can modify their contents by adding, removing, or changing elements. Lists are defined using square brackets [].
Here’s an overview of lists and some example methods:
2.0.2.2 Common List Methods:
- Accessing
## 1
## [2, 3, 'apple']
- Append (
append()):- Adds an element to the end of the list.
## [1, 2, 3, 'apple', 'banana', 'XXXX']
- Extend (
extend()):- Extends the list by appending elements from another iterable.
## [1, 2, 3, 'apple', 'banana', 'XXXX', 5, 6, 7]
- Insert (
insert()):- Inserts an element at a specified position.
- this is not replacing !!!
- Inserts an element at a specified position.
## [1, 2, 'orange', 3, 'apple', 'banana', 'XXXX', 5, 6, 7]
- Remove (
remove()):- Removes the first occurrence of a specified value.
## [1, 2, 'orange', 3, 'apple', 'XXXX', 5, 6, 7]
Pop (
pop()):- Removes and returns the element at the specified index. If no index is provided, it removes the last element.
## orange
- Index (
index()):- Returns the index of the first occurrence of a specified value.
## 3
- Count (
count()):- Returns the number of occurrences of a specified value.
## 0
Sort (
sort()):- Sorts the list in ascending order. Optionally, you can specify
reverse=Truefor descending order.
- Sorts the list in ascending order. Optionally, you can specify
# Convert all elements to strings before sorting
st_list = [str(x) for x in my_list]
st_list.sort()
print(st_list)## ['1', '2', '3', '5', '6', '7', 'XXXX', 'apple']
# Convert all elements to strings before sorting
sorted_list = sorted(map(str, my_list))
print(sorted_list)## ['1', '2', '3', '5', '6', '7', 'XXXX', 'apple']
Reverse (
reverse()):- Reverses the order of the elements in the list.
## [7, 6, 5, 'XXXX', 'apple', 3, 2, 1]
- Concatenate lists
## [1, 2, 3, 'x', 'y', 'z']
loop thru a list
## A
## B
## C
2.0.3 tuples
Tuples are an ordered sequences of items, just like lists. The main difference between tuples and lists is that tuples cannot be changed (immutable) unlike lists which can (mutable).
tuples are collection of different type of objects.
Empty tuples
Create tuples
## (1,)
Concatenate tuples
## (1, 2.5, 'string', [3, 4], 'a', 'ab')
Immutable
element of a tuple can not be changed
sorted function
sorted() function: We can sort the tuple and assign a new name
sorted() function returns list type.
## [1, 2, 4, 5, 7]
Nesting
we can create nested tuples.
## ('a', 'b', 'c')
2.0.4 Sets
Sets in Python are unordered collections of unique elements.
Unlike lists, sets do not have a specific order, and each element in a set must be unique. Here are some common operations and methods associated with sets:
- Creating Sets
- Adding Elements
- Removing Elements
- Set Operations
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# Union
union_set = set1.union(set2) # Result: {1, 2, 3, 4, 5, 6, 7}
# Intersection
intersection_set = set1.intersection(set2) # Result: {3, 4, 5}
# Difference
difference_set = set1.difference(set2) # Result: {1, 2}
# Symmetric Difference
symmetric_difference_set = set1.symmetric_difference(set2) # Result: {1, 2, 6, 7}2.0.4.1 Set Methods
# Discard an element (if it exists)
my_set.discard(4)
# Clear all elements
my_set.clear()
# Copying a set
copy_of_set = my_set.copy()- Other Operations
# Checking Membership
is_present = 5 in my_set # Result: True
# Length of a Set
length_of_set = len(my_set)Sets are useful when you need to work with unique elements, perform set operations, or check for membership efficiently. They do not support indexing or slicing, as they are unordered. Additionally, sets are mutable, meaning you can add and remove elements, but individual elements must be immutable (e.g., numbers, strings).
2.0.5 Dictionary
Dictionaries in Python are collections of key-value pairs, where each key must be unique. Dictionaries are versatile and used for mapping values to unique keys. Here are common operations and methods associated with dictionaries:
- Creating Dictionaries
## {'name': 'John', 'age': 30, 'city': 'New York'}
- Accessing Values
## John
## John
- Modifying Dictionaries
- Removing Items
my_dict = {'name': 'John',
'age': 30,
'gender': 'Male'}
# Using the pop() method
gender = my_dict.pop('gender') # Removes the 'gender' key and returns its value
print(gender)## Male
2.0.7 Other Operations:
# Checking Membership (in terms of keys)
is_present = 'age' in my_dict # Result: True
# Length of a Dictionary
length_of_dict = len(my_dict)Dictionaries are widely used in Python for tasks that involve mapping keys to values, such as representing data structures, configuration settings, and more. They are mutable, meaning you can modify their contents by adding, updating, or removing key-value pairs. The keys must be immutable (e.g., strings, numbers), but the values can be of any type.