Lists & Stacks
Lists are a fundamental data structure in Python that can be used to store a collection of items. They are dynamic, meaning they can grow and shrink in size as needed. Stacks are a specific type of list that follows the Last In, First Out (LIFO) principle, where the last item added is the first one to be removed.
Slicing
Defining a list
| fruits = ["apple", "banana", "cherry", "kiwi", "orange"]
fruits
|
['apple', 'banana', 'cherry', 'kiwi', 'orange']
Accessing element with index 0
Accessing element with index 2
Accessing last element
Slicing from index 1 to 3
Replacing the second element with 'watermelon
| fruits[1] = "watermelon"
fruits
|
['apple', 'watermelon', 'cherry', 'kiwi', 'orange']
| numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
| print(numbers[2:5]) # From index 2 to index 5
print(numbers[:5]) # From the start to index 5
print(numbers[5:]) # From index 5 to the end
print(numbers[::2]) # Step size of 2
print(numbers[::-1]) # Reverse the list
print(numbers[::-2]) # Reverse the list with step size of 2
|
[3, 4, 5]
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 3, 5, 7, 9]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[10, 8, 6, 4, 2]
Methods
| fruits = ["apple", "banana", "cherry", "kiwi", "orange"]
|
Adding an element to the end of the list with append
| fruits.append("lemon")
fruits
|
['apple', 'banana', 'cherry', 'kiwi', 'orange', 'lemon']
Adding 'grape' to the end of the list
| fruits.insert(1, "grape")
fruits
|
['apple', 'grape', 'banana', 'cherry', 'kiwi', 'orange', 'lemon']
Remove first occurrence of cherry
| fruits.remove("cherry") # remove first occurrence
fruits
|
['apple', 'grape', 'banana', 'kiwi', 'orange', 'lemon']
Removing last element from list
| popped_fruits = fruits.pop() # remove last item
fruits, popped_fruits
|
(['apple', 'grape', 'banana', 'kiwi', 'orange'], 'lemon')
Getting 'banana' index
| banana_index = fruits.index("banana") # find index of "cherry"
banana_index
|
Counting 'kiwi' occurrences
| fruits.insert(2, "kiwi") # insert "kiwi" at index 2
kiwi_count = fruits.count("kiwi") # count occurrences of "kiwi"
kiwi_count
|
Sort fruits
| fruits.sort() # sort the list in ascending order
fruits
|
['apple', 'banana', 'grape', 'kiwi', 'kiwi', 'orange']
Sort fruits
| fruits.reverse() # sort the list in descending order
fruits
|
['orange', 'kiwi', 'kiwi', 'grape', 'banana', 'apple']
Remove all elements from the list
| fruits.clear() # sort the list in descending order
fruits
|
Iterating over a list
Iterate over a list of numbers
| numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers:
print(number, end=" ") # Print each number in the list
|
Iterate over a list of numbers printing index and value
| for index, number in enumerate(numbers):
print(f"Index: {index}, Number: {number}")
|
Index: 0, Number: 1
Index: 1, Number: 2
Index: 2, Number: 3
Index: 3, Number: 4
Index: 4, Number: 5
Index: 5, Number: 6
Index: 6, Number: 7
Index: 7, Number: 8
Index: 8, Number: 9
Index: 9, Number: 10
List comprehension to create a new list with squares of numbers
| squares = [number**2 for number in range(1, 11)]
|
List comprehension to create a new list with even numbers
| even = [number for number in range(1, 11) if number % 2 == 0]
|
Nested list comprehension
| lst1 = [1, 2, 3, 4]
lst2 = ["a", "b", "c", "d"]
nested_list = [[i, j] for i in lst1 for j in lst2]
nested_list
|
[[1, 'a'],
[1, 'b'],
[1, 'c'],
[1, 'd'],
[2, 'a'],
[2, 'b'],
[2, 'c'],
[2, 'd'],
[3, 'a'],
[3, 'b'],
[3, 'c'],
[3, 'd'],
[4, 'a'],
[4, 'b'],
[4, 'c'],
[4, 'd']]
List comprehension with function calls
| words = ["hello", "world", "python"]
length_words = [len(word) for word in words]
length_words
|