Lists
1. Introduction to Lists
A list is an ordered collection of data values.
The values in a list are called elements or items.
Lists are versatile; they can hold elements of different data types, unlike strings,
where each character must be a string.
2. Creating Lists
To create a new list, enclose the elements in square brackets [].
my_list = [1, 2, 'hello', True]
3. Accessing List Elements and Traversal
List indices work similarly to string indices. Use the subscript operator [ ] to access
elements.
my_list = [1, 2, 'hello', True]
my_list[0] # Access the first element
my_list[-1] # Access the last element
4. List Comprehension
List comprehension allows creating a new list by applying an expression to each
element in an existing list.
1. squared = [x**2 for x in range(5)]
2. list_1 = [1,2,3,4,5,6,7,8,9,10]
selected_list = [x for x in list_1 if x % 2 == 0]
5. List Operations
+ operator: Concatenates lists.
list_a = [1, 2, 3]
list_b = ["a","b",77]
combined_list = list_a + list_b
print(combined_list) # Output: [1, 2, 3, 4, 5, 6]
* operator: Repeats a list a specified number of times.
list_a = [1, 2, 3]
repeated_list = list_a * 3
print(repeated_list) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
Relational operators (like >, <) compare lists lexicographically (i.e., element by
element).
list_x = [1, 2, 3]
list_y = [1, 2, 4]
print(list_x < list_y) # Output: True, because 3 < 4 in the first
# non-matching pair
list_a = [2, 3, 5]
list_b = [2, 3, 5, 7]
print(list_a < list_b) # Output: True, because list_a is shorter and
# identical in elements up to its length
list_c = [3, 4, 5]
list_d = [3, 2, 5]
print(list_c > list_d) # Output: True, because 4 > 2 in the second
# position
list_c = [3, 4, 5]
list_d = [3, 4, 5]
print(list_c == list_d) # True
6. Membership Operators
The in operator checks for the presence of an element in a list.
List_1 = [2, 4, 6]
print(2 in list_1) # Output: True
7. List Slices
Slices can be used to access sub-parts of a list, similar to slicing in strings.
my_list = [1,2,3,"a","b","c"]
sublist = my_list[1:3]
8. List Mutability
Lists are mutable, allowing elements to be added, removed, or modified.
9. Using the Slice Operator for List Mutations
Replacing elements: Use slices to replace elements in a list.
composite = [13, 17, 19, 23, 25]
composite[4:] = [29, 31] #output : [13, 17, 19, 23, 29, 31]
Inserting elements: The slice operator can insert elements at a specified position.
prime = [2, 3, 5, 7]
prime[4:4] = [11] #insert „11‟ at index 4
Removing elements: Set a slice to an empty list to remove elements.
composite = [3, 5, 7, 9]
composite[2:3] = []
10. Using del Keyword for Deletion
The del keyword can delete elements or slices directly.
my_list = [“a”,1,3,45,56,”x”]
del my_list[2] # ['a', 1, 45, 56, 'x']
11. Common List Methods
list.index(element): Returns the position of element.
list.insert(position, element): Inserts element at position.
list.append(element): Adds element at the end.
list.extend(aList): Appends all elements from aList.
list.remove(element): Removes element from L.
list.pop(): Removes and returns the last element.
list.sort(): Sorts the list.
list.reverse(): Reverses the list.
13. Strings and Lists
Converting strings to lists:
char_list = list("hello")
Splitting a string into words:
words = "hello world".split()
14. List of Lists (Nested Lists)
A list can contain other lists
Example 1:
list_1 = [1 , [“a” ,”b” ,”c”] , 7]
a b c
1 0 1 2
7
0 1 2
Accessing elements:
list[0] = 1
list[1][0] = a
list[1][1] = b
list[1][2] = c
list[2] = 7
Example 2:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
15. List Aliasing and Cloning
Aliasing: Assigning a list to another variable creates a reference to the same list.
alias_list = original_list
Cloning: Creating a copy using slicing [:].
cloned_list = original_list[:]
Equality and Identity in Lists
o is checks identity (if two variables point to the same object).
o == checks structural equivalence (if two lists have the same elements).