This lecture for CSEN102: Introduction to Computer Science, given by Milad Ghantous, PhD, is a continuation of the topic of lists, with a specific focus on their implementation and usage in Python.
As discussed in previous lectures, a list (or array in some languages) is a data structure used to store a collection of items. In Python, lists are versatile and can hold items of different data types, making them powerful for various programming tasks.
Lists in Python are created by placing all the items (elements) inside square brackets `[]`, separated by commas.
my_list = []
numbers = [1, 2, 3, 4, 5]
Python lists can contain elements of different data types within the same list.
mixed_list = [1, "hello", 3.14, True]
A list can also contain other lists as elements, creating nested structures (like a 2D array).
nested_list = [1, 2, [3, 4]]
Elements in a Python list are accessed using indexing. Positive indices start from 0 for the first element, and negative indices can be used to access elements from the end of the list (-1 for the last element).
my_list = ['p', 'y', 't', 'h', 'o', 'n']
print(my_list[0]) # Output: p
print(my_list[2]) # Output: t
my_list = ['p', 'y', 't', 'h', 'o', 'n']
print(my_list[-1]) # Output: n
print(my_list[-3]) # Output: h
You can access a range of elements in a list using the slicing operator `[:]`.
my_list = ['p', 'y', 't', 'h', 'o', 'n']
print(my_list[1:4]) # Output: ['y', 't', 'h']
print(my_list[:3]) # Output: ['p', 'y', 't']
print(my_list[3:]) # Output: ['h', 'o', 'n']
Since Python lists are mutable, you can change their elements, add new ones, or remove existing ones.
my_list = [1, 2, 3]
my_list[0] = 5
print(my_list) # Output: [5, 2, 3]
append(): Adds an element to the end of the list.
my_list.append(4)
print(my_list) # Output: [5, 2, 3, 4]
insert(): Inserts an element at a specified index.
my_list.insert(1, 10)
print(my_list) # Output: [5, 10, 2, 3, 4]
del statement: Deletes an element at a specific index or a slice.
del my_list[1]
print(my_list) # Output: [5, 2, 3, 4]
remove(): Removes the first occurrence of a specified value.
my_list.remove(3)
print(my_list) # Output: [5, 2, 4]
pop(): Removes and returns the element at a specified index (or the last element if no index is given).
last_element = my_list.pop()
print(my_list) # Output: [5, 2]
print(last_element) # Output: 4
clear(): Removes all items from the list.
my_list.clear()
print(my_list) # Output: []
Python lists have several built-in methods for common operations.
len(): Returns the number of elements in the list.
numbers = [1, 2, 3]
print(len(numbers)) # Output: 3
sort(): Sorts the items of the list in ascending order by default.
numbers = [3, 1, 2]
numbers.sort()
print(numbers) # Output: [1, 2, 3]
reverse(): Reverses the order of elements in the list.
numbers = [1, 2, 3]
numbers.reverse()
print(numbers) # Output: [3, 2, 1]
count(): Returns the number of times a specified value appears in the list.
my_list = [1, 2, 2, 3]
print(my_list.count(2)) # Output: 2
index(): Returns the index of the first occurrence of a specified value.