Skip to content

Lists

Slicing

fruits = ["apple", "banana", "cherry", "kiwi", "orange"]
fruits

['apple', 'banana', 'cherry', 'kiwi', 'orange']

print("Accessing element with index 0:")
fruits[0]

Accessing element with index 0:

'apple'

print("Accessing element with index 2:")
fruits[2]

Accessing element with index 2:

'cherry'

print("Accessing last element:")
fruits[-1]

Accessing last element:

'orange'

print("Slicing from index 1 to 3:")
fruits[1:3]

Slicing from index 1 to 3:

['banana', 'cherry']

1
2
3
print("Replacing the second element with 'watermelon':")
fruits[1] = "watermelon"
fruits

Replacing the second element with 'watermelon':

['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]

1
2
3
4
5
6
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"]
1
2
3
print("Adding 'grape' to the end of the list:")
fruits.insert(1, "grape")
fruits

Adding 'grape' to the end of the list:

['apple', 'grape', 'banana', 'cherry', 'kiwi', 'orange']

1
2
3
print("Remove first occurrence of cherry:")
fruits.remove("cherry")  # remove first occurrence
fruits

Remove first occurrence of cherry:

['apple', 'grape', 'banana', 'kiwi', 'orange']

1
2
3
4
print("Removing last element from list:")
popped_fruits = fruits.pop()  # remove last item
print(popped_fruits)
fruits

Removing last element from list:

orange

['apple', 'grape', 'banana', 'kiwi']

1
2
3
print("Getting 'banana' index:")
banana_index = fruits.index("banana")  # find index of "cherry"
banana_index

Getting 'banana' index:

2

1
2
3
4
print("Counting 'kiwi' occurrences:")
fruits.insert(2, "kiwi")  # insert "kiwi" at index 2
kiwi_count = fruits.count("kiwi")  # count occurrences of "kiwi"
kiwi_count

Counting 'kiwi' occurrences:

2

1
2
3
print("Sort fruits: ")
fruits.sort()  # sort the list in ascending order
fruits

Sort fruits:

['apple', 'banana', 'grape', 'kiwi', 'kiwi']

1
2
3
print("Sort fruits: ")
fruits.reverse()  # sort the list in descending order
fruits

Sort fruits:

['kiwi', 'kiwi', 'grape', 'banana', 'apple']

1
2
3
print("Remove all elements from the list: ")
fruits.clear()  # sort the list in descending order
fruits

Remove all elements from the list:

[]

Iterating over a list

1
2
3
4
print("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:

1 2 3 4 5 6 7 8 9 10

1
2
3
print("Iterate over a list of numbers printing index and value:")
for index, number in enumerate(numbers):
    print(f"Index: {index}, Number: {number}")

Iterate over a list of numbers printing index and value:

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

print("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 squares of numbers:

print("List comprehension to create a new list with even numbers:")
even = [number for number in range(1, 11) if number % 2 == 0]

List comprehension to create a new list with even numbers:

1
2
3
4
5
print("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

Nested list comprehension:

[[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']]

1
2
3
4
print("List comprehension with function calls:")
words = ["hello", "world", "python"]
length_words = [len(word) for word in words]
length_words

List comprehension with function calls:

[5, 5, 6]