Module 3.2.1 Lists PDF
Module 3.2.1 Lists PDF
Page 2
Module 3
► SELECTION AND ITERATION USING PYTHON:- if-else, elif, for loop, range, while loop.
► SEQUENCE DATA TYPES IN PYTHON - list, tuple, set, strings, dictionary, Creating and
using Arrays in Python (using Numpy library).
Page 3
Python Lists
Page 4
A List is a Kind of Collection
► A list is an ordered set of data values. The values that make up a list are
called its elements or items.
Page 4
List Constants
► A list element can be any Python object >>> print([ 1, [5, 6], 7])
- even another list [1, [5, 6], 7]
>>> print([])
► A list can be empty []
Page 5
List Constants
>>> digits=list(range(10))
>>> print(digits)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> mul5=list(range(10,50,5))
>>> print(mul5)
[10, 15, 20, 25, 30, 35, 40, 45]
Page 6
Looking Inside Lists
0 1 2
Page 7
We Already Use Lists!
for i in [5, 4, 3, 2, 1] : 5
print(i) 4
3
2
1
Page 8
How Long is a List?
Page 9
Lists and Definite Loops
Page 10
List operations
Page 12
Concatenating Lists Using +
Page 12
Repeats a List Using *
Page 13
Equality operator works well on lists
>>> composite=[4,6,8]
>>> print(even==composite)
False
Page 14
Relational operators also work with lists
>>> prime=[2,3,5] ► Python starts by comparing the first element from each
>>> even=[2,4,6] list.
>>> print(prime>even)
► If they are equal, it goes on to the next element, and so
False
on, until it finds the first pair of elements that are
different and determines the relation between them.
► In this example, prime[0] == even[0].
► Next, prime[1] and even[1] are compared.
► Thus, the resulting relation is ‘<’ and prime > even is thus
False.
► Once the result is determined, the subsequent elements are
skipped.
Page 15
Membership operators can be applied to a list as well
>>> even=[2,4,6,8]
>>> composite=[4,6,8]
>>> print(2 in even)
True
>>> print(2 in composite)
False
Page 16
Lists Can Be Sliced Using :
>>> t[:4]
[9, 41, 12, 3] ► Remember: Just like in strings, the
second number is “up to but not
>>> t[3:] including”
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
Page 17
Lists Can Be Sliced Using :
last_three = names[-3:]
print(last_three)
Page 18
Lists are Mutable
Page 19
Slice operator and mutations
► In Python, the slice operator can be used not only for accessing portions of
sequences but also for modifying mutable sequences like lists.
Page 20
Replacing Elements Using the Slice Operator
Page 21
Replacing Elements Using the Slice Operator
Page 22
Inserting Elements Using the Slice Operator
► You can insert new elements into a list at any position by assigning values to an empty slice
(start:stop with start == stop).
► numbers = [1, 2, 3, 4, 5]
► # Inserting at the beginning
► numbers[0:0] = [10]
► print(numbers) # Output: [10, 1, 2, 3, 4, 5]
Page 26
List Methods
Method Description
index() Returns the index of the first occurrence of the specified value.
Page 26
List Methods
Method Description Example
► Returns the index of the first occurrence of a
specified value. numbers = [10, 20, 30, 40, 50]
index() ► Syntax: list.index(value) print(numbers.index(30))
► Raises: ValueError if the specified value is not # Output: 2
found.
numbers = [10, 20, 30, 40, 50]
# Insert 25 at index 2
numbers.insert(2, 25)
► Inserts an element at a specified position. print(numbers)
insert()
► Syntax: list.insert(index, value) # Output: [10, 20, 25, 30, 40,
50]
Page 27
List Methods
Method Description Example
numbers = [10, 20, 30]
# Adds 40 to the end of the
► Adds an element at the end of the list. list
append() numbers.append(40)
► Syntax: list.append(value)
print(numbers)
# Output: [10, 20, 30, 40]
numbers = [10, 20, 30]
# Adds all elements from the
► Extends the list by appending elements from second list
numbers.extend([40, 50, 60])
extend() another list (or any iterable). print(numbers)
► Syntax: list.extend(iterable) # Output: [10, 20, 30, 40, 50,
60]
Page 28
List Methods
Method Description Example
► Removes the first occurrence of a specified numbers = [10, 20, 30, 40, 30]
value. # Removes the first occurrence of
30
remove() ► Syntax: list.remove(value) numbers.remove(30)
► Raises: ValueError if the specified value is print(numbers)
not found. # Output: [10, 20, 40, 30]
► Returns the number of occurrences of a numbers = [10, 20, 30, 10, 20, 10]
print(numbers.count(10))
count() specified value.
# Output: 3
► Syntax: list.count(value)
Page 29
List Methods
Method Description Example
numbers = [10, 20, 30, 40, 50]
Page 30
List Methods
Method Description Example
numbers = [50, 10, 40, 30, 20]
# Sorts in ascending order
numbers.sort()
► Sorts the list in ascending order by default print(numbers)
(can be customized to sort in descending # Output: [10, 20, 30, 40, 50]
sort()
order).
► Syntax: list.sort(reverse=False) # Sort in descending order
numbers.sort(reverse=True)
print(numbers)
# Output: [50, 40, 30, 20, 10]
Page 31
List Methods
Method Description Example
numbers = [10, 20, 30, 40, 50]
# Reverses the list
► Reverses the elements of the list in place.
reverse() numbers.reverse()
► Syntax: list.reverse() print(numbers)
# Output: [50, 40, 30, 20, 10]
Page 32
Best Friends: Strings and Lists
s = "hello"
lst = list(s)
print(lst) # Output: ['h', 'e', 'l', 'l', 'o']
► The list() method converts an iterable (like a string) into a list of its
elements
► When used with a string, it breaks the string down into a list of individual
characters
Page 33
Best Friends: Strings and Lists
Page 34
Exercises - List Slicing
Page 35
Exercises - List Methods (Append, Insert, Remove)
Page 36
Exercises - List Methods (Append, Insert, Remove)
Page 37
Exercises - Find the Largest Number in a List
► Objective: How to loop through a list and find the largest element.
► Instructions:
► Create a list of numbers: [12, 75, 34, 99, 45, 67].
Page 38
Exercises - Find the Largest Number in a List
► Objective: How to loop through a list and find the largest element.
► Instructions:
► Create a list of numbers: [12, 75, 34, 99, 45, 67].
Page 39
Exercises - Counting Occurrences in a List
Page 40
Exercises - Reversing a List
► Objective: Use the reverse() method to reverse the order of elements in a list.
► Instructions:
► Create a list of numbers: [10, 20, 30, 40, 50].
Page 41
Exercises - Remove Duplicates from a List
► Loop through the original list, and for each element, check if it is already in the new
list.
► If the element is not in the new list, add it.
Page 42