Pyhon Module 5
Pyhon Module 5
Note: The length of an array is always one more than the highest array index.
Example:
cars = ["Ford", "Volvo", "BMW"]
cars[3] = "Toyota“
print(cars) #[' Ford ', 'Volvo', 'BMW‘, ‘Toyota’]
Unit 5 Array Techniques / 8
Introduction to Array Techniques
• Removing Array Elements
• You can use the pop() method to remove an element from the array.
• Example
• Delete the second element of the cars array:
cars = ["Ford", "Volvo", "BMW"]
cars.pop(1)
print(cars) # ['Ford', 'BMW']
• You can also use the remove() method to remove an element from the array.
• Delete the element that has the value "Volvo":
cars = ["Ford", "Volvo", "BMW"]
cars.remove("Volvo")
print(cars) # ['Ford', 'BMW']
Unit 5 Array Techniques / 9
Array Methods
• Python has a set of built-in methods that you can use on lists/arrays.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Unit 5 Array Techniques / 10
Unit 5 Array Techniques / 11
Introduction to Array Techniques
• Python List append() Method
• Example
• Add an element to the fruits list:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits) # ['apple', 'banana', 'cherry', 'orange']
----
a = ["apple", "banana", "cherry"]
b = ["Ford", "BMW", "Volvo"]
a.append(b)
print(a) # ['apple', 'banana', 'cherry', ["Ford", "BMW", "Volvo"]]
• The count() method returns the number of elements with the specified value.
• Return the number of times the value 9 appears int the list:
fruits = [1, 4, 2, 9, 7, 8, 9, 3, 1]
x = fruits.count(9)
print(x) #2
Note: The index() method only returns the first occurrence of the value.
• More details
• https://www.w3schools.com/python/python_sets.asp
• https://www.geeksforgeeks.org/remove-duplicates-sorted-array/
Output:
1 2 3 4 5
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
Method Description
index() Searches the tuple for a specified value and returns the position of
where it was found
Set Operations
Method Description
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another, specified set
intersection_update() Removes the items in this set that are not present in other, specified set(s)
symmetric_difference_update() inserts the symmetric differences from this set and another
update() Update the set with another set, or any other iterable
Dictionary Operations
Method Description
items() Returns a list containing a tuple for each key value pair
setdefault() Returns the value of the specified key. If the key does not exist: insert the key,
with the specified value
islower() Returns True if all characters in the string are lower case
isupper() Returns True if all characters in the string are upper case
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the position of where it
was found
String Operations
Method Description
replace() Returns a string where a specified value is replaced with a specified value
split() Splits the string at the specified separator, and returns a list
split() Splits the string at the specified separator, and returns a list
Some Examples
Time Tradeoff
Sometimes the choice of an algorithm that involves a space-time tradeoff. That is by increasing the amount
of space for storing the data, we may be able to reduce the time needed for processing data or vice-versa.
Basically it is a way of solving problems in:
1. Less time by using more memory
2. Solving a problem in little space by spending a long time.