Python Lists
20/10/2025
Lists
• Lists are used to store multiple items in a single variable.
• Lists are one of 4 built-in data types in Python used to store
collections of data, the other 3 are Tuple, Set, and Dictionary, all with
different qualities and usage.
• Lists are created using square brackets
Creating a list:
my_list = [1, 2, 3, 4, 5]
mixed_list = [1, "apple", 3.14, True]
empty_list = []
print(my_list)
print(mixed_list)
print(empty_list)
List Items
• List items are ordered, changeable, and allow duplicate values.
• List items are indexed, the first item has index [0], the second item
has index [1] etc.
Ordered
• When we say that lists are ordered, it means that the items have a
defined order, and that order will not change.
• If you add new items to a list, the new items will be placed at the end
of the list.
Changeable & Duplicate
• The list is changeable, meaning that we can change, add, and remove
items in a list after it has been created.
• Since lists are indexed, lists can have items with the same value.
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
mylist = ["Mathematics", "Physics", "Psychology", "Philosophy", "Chemistry", "Mathematics"]
mylist[0] = 'Astronomy'
print(mylist)
Access Items
• List items are indexed and you can access them by referring to the
index number:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Negative Indexing
• Negative indexing means start from the end
• -1 refers to the last item, -2 refers to the second last item etc.
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
Range of Indexes
• You can specify a range of indexes by specifying where to start and where to
end the range.
• When specifying a range, the return value will be a new list with the specified
items.
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
Note: The search will start at index 2 (included) and end at index 5 (not
included).
Example
• This example returns the items from the beginning to,
but NOT including, "kiwi":
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])
Another example:
• By leaving out the end value, the range will go on to the end of the
list.
• This example returns the items from "cherry" to the end:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:])
Range of Negative Indexes
• Specify negative indexes if you want to start the search from the end
of the list
• This example returns the items from "orange" (-4) to, but NOT
including "mango" (-1)
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])
Slicing Syntax
• If L is a list, the expression L [ start : stop : step ] returns the
portion of the list from index start to index stop, at a step
size step.
Slicing examples:
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i’]
print(L[2:7])
# Prints ['c', 'd', 'e', 'f', 'g']
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i’]
print(L[-7:-2])
# Prints ['c', 'd', 'e', 'f', 'g']
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i’]
print(L[2:-5])
# Prints ['c', 'd']
Slicing examples:
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i’]
print(L[2:7:2])
# Prints ['c', 'e', 'g']
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i’]
print(L[6:1:-1])
# Prints ['g', 'f', 'e', 'd', 'c']
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i’]
print(L[::-1])
What will be the result??
Check if Item Exists
• To determine if a specified item is present in a list use the in keyword:
• Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
Change List Items
• To change the value of a specific item, refer to the index number:
• Change the second item:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
Change a Range of Item Values
• To change the value of items within a specific range, define a list with the
new values, and refer to the range of index numbers where you want to
insert the new values
• Change the values "banana" and "cherry" with the values "blackcurrant"
and "watermelon":
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
Another example of changing a range
of items
• If you insert more items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly
• Change the second value by replacing it with two new values:
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = [“losh", "watermelon"]
print(thislist)
Note: The length of the list will change when the number of items inserted
does not match the number of items replaced.
Another example:
• If you insert less items than you replace, the new items will be
inserted where you specified, and the remaining items will move
accordingly
• Change the second and third value by replacing it with one value:
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist)
Insert Items
• To insert a new list item, without replacing any of the existing values,
we can use the insert() method.
• The insert() method inserts an item at the specified index
thislist = ["apple", "banana", "cherry"]
[Link](2, "watermelon")
print(thislist)
Extend List
• To append elements from another list to the current list, use the extend() method.
• Add the elements of tropical to thislist:
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
[Link](tropical)
print(thislist)
The elements will be added to the end of the list.
Add Any Iterable
• The extend() method does not have to append lists, you can add any
iterable object (tuples, sets, dictionaries etc.).
thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
[Link](thistuple)
print(thislist)
Remove List Items
• The remove() method removes the specified item.
thislist = ["apple", "banana", "cherry"]
[Link]("banana")
print(thislist)
Remove Specified Index
• The pop() method removes the specified index.
thislist = ["apple", "banana", "cherry"]
[Link](1)
print(thislist)
If you do not specify the index, the pop() method removes the last
item.
The del keyword also removes the
specified index:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
thislist = ["apple", "banana", "cherry"]
del thislist[0],thislist[1]
print(thislist)
If you write the list name with del keyword
del thislist
it will delete the entire list, permanently
Clear the List
• The clear() method empties the list.
• The list still remains, but it has no content.
• Clear the list content:
thislist = ["apple", "banana", "cherry"]
[Link]()
print(thislist)
Next will be…
Looping lists,
List comprehension
Sorting lists
Copy lists
Join lists
List methods
Loop Lists:
• Loop Through a List
You can loop through the list items by using a for loop:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
Loop Through the Index Numbers:
• You can also loop through the list items by referring to their index
number.
• Use the range() and len() functions to create a suitable iterable.
Using a While Loop:
• You can loop through the list items by using a while loop.
• Use the len() function to determine the length of the list, then start at 0
and loop your way through the list items by referring to their indexes.
• Remember to increase the index by 1 after each iteration.
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
print(thislist[i])
i = i + 1
Looping Using List Comprehension
• List Comprehension offers the shortest syntax for looping through
lists:
• This is a short hand for loop that will print all items in a list:
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]
List Comprehension
• List comprehension offers a shorter syntax when you want to create a
new list based on the values of an existing list.
Example:
• Based on a list of fruits, you want a new list, containing only the fruits
with the letter "a" in the name.
• Without list comprehension you will have to write a for statement
with a conditional test inside:
Example:
fruits =
["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
[Link](x)
print(newlist)
Here is the joy …..
• With list comprehension you can do all that with only one line of
code:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
Lets go deeper in comprehension:
• The Syntax
newlist = [expression for item in Iterable if condition == True]
expression: Defines how each element in the new list should look.
item: Represents each element in the iterable.
iterable: The collection we’re looping over (like a list or range).
condition (optional): Filters items in the iterable, adding only those that satisfy
the condition
Condition:
The condition is like a filter that only accepts the items that valuate to True.
• Only accept items that are not "apple":
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if x != "apple"]
The condition if x != "apple" will return True for all elements other than "apple", making
the new list contain all fruits except "apple".
The condition is optional and can be omitted
Example:
With no if statement:
newlist = [x for x in fruits]
This will copy all fruits and add in newlist.
Iterable
• The iterable can be any iterable object, like a list, tuple, set etc.
You can use the range() function to create an iterable:
newlist = [x for x in range(10)]
Some More Examples:
• newlist1 = [x for x in range(2,20,3)]
• newlist3 = [x for x in range(-100, -20, 1)]
• newlist4 = [x for x in range(10, 0, -1)]
• newlist2 = [x for x in range(2,20,3) if x%2==0]
• print(newlist1)
• print(newlist2)
• print(newlist3)
• print(newlist4)
Example
• Accepts only numbers lower than 5:
newlist = [x for x in range(10) if x < 5]
Expression:
• The expression is the current item in the iteration, but it is also the
outcome, which you can manipulate before it ends up like a list item
in the new list:
• Set the values in the new list to upper case:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [[Link]() for x in fruits]
List 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