Python - Lists
count()
method in Python lists?The count()
method in Python lists is used to count the number of occurrences of a specified element in the list. It returns the count of how many times the specified element appears in the list.
# Creating a list with repeated elements
my_list = [1, 2, 3, 2, 4, 2, 5]
# Counting the occurrences of the element 2
count_of_2 = my_list.count(2)
# Displaying the count
print(count_of_2) # Output: 3
The program initializes a list (my_list
) with repeated elements and uses the count()
method to count the occurrences of the element 2
in the list.
Output:
3
In Python, you can reverse the order of elements in a list using the reverse()
method. This method modifies the original list in-place by reversing the order of its elements.
# Creating a list
my_list = [1, 2, 3, 4, 5]
# Reversing the order of elements using the reverse() method
my_list.reverse()
# Displaying the updated list
print(my_list) # Output: [5, 4, 3, 2, 1]
The program initializes a list (my_list
) and uses the reverse()
method to reverse the order of its elements.
Output:
[5, 4, 3, 2, 1]
sort()
method for sorting elements in a list.The sort()
method in Python lists is used to sort the elements of a list in ascending order. It modifies the original list in-place. If you want to sort the list in descending order, you can use the reverse=True
parameter.
# Creating a list
numbers = [4, 2, 8, 1, 5]
# Sorting the list in ascending order using the sort() method
numbers.sort()
# Displaying the sorted list
print(numbers) # Output: [1, 2, 4, 5, 8]
The program initializes a list (numbers
) and uses the sort()
method to sort its elements in ascending order. The original list is modified in-place.
Output:
[1, 2, 4, 5, 8]
If you want to sort the list in descending order, you can use the reverse=True
parameter:
# Creating a list
numbers = [4, 2, 8, 1, 5]
# Sorting the list in descending order using the sort() method
numbers.sort(reverse=True)
# Displaying the sorted list in descending order
print(numbers) # Output: [8, 5, 4, 2, 1]
Output:
[8, 5, 4, 2, 1]
In Python, the difference between shallow copy and deep copy for lists lies in how they handle nested objects within the list. Let's explore both concepts with examples:
Shallow Copy:
A shallow copy creates a new list, but it does not create copies of the objects within the original list. Instead, it copies references to the objects. Therefore, changes made to the nested objects inside the copied list will affect both the original and copied lists.
import copy
# Creating a list with nested objects
original_list = [1, [2, 3], [4, 5]]
# Creating a shallow copy
shallow_copy = copy.copy(original_list)
# Modifying a nested object in the shallow copy
shallow_copy[1][0] = 999
# Displaying both lists
print(original_list) # Output: [1, [999, 3], [4, 5]]
print(shallow_copy) # Output: [1, [999, 3], [4, 5]]
Deep Copy:
A deep copy creates a new list and recursively creates copies of all nested objects within the original list. This means changes made to nested objects in the copied list won't affect the original list.
import copy
# Creating a list with nested objects
original_list = [1, [2, 3], [4, 5]]
# Creating a deep copy
deep_copy = copy.deepcopy(original_list)
# Modifying a nested object in the deep copy
deep_copy[1][0] = 999
# Displaying both lists
print(original_list) # Output: [1, [2, 3], [4, 5]]
print(deep_copy) # Output: [1, [999, 3], [4, 5]]
The key difference is that changes to nested objects in a deep copy don't affect the original list, while changes in a shallow copy do.
Outputs:
[1, [999, 3], [4, 5]] [1, [999, 3], [4, 5]]
[1, [2, 3], [4, 5]] [1, [999, 3], [4, 5]]