Python Lists: Unleash the
Power!
Python lists are the backbone of many programs. Learn to
wield them effectively!
by Team Piyush:-
Members –
Piyush(Mentor)
Yogesh
Divine
Parthik
Sourav
Introduction to Lists
What is a List?
•A list is a collection of items stored in a variable.
•Lists are ordered, mutable (modifiable), and allow duplicate values.
Containers
Key Properties:
List Syntax:
✔ Ordered (items remain in the same sequence)
my_list = [1, 2, 3, 4, 5] # List with numbers ✔ Mutable (can be changed)
fruits= ["apple", "banana", "cherry"] # List with strings
mixed = [1, "hello", 3.14, True] # List with mixed data types ✔ Can store different data types
✔ Allows duplicate values
List Operations
1. Adding Elements
•Append(value): Adds an element at the end.
•Insert(index, Value): Adds an element at a specific
position.
•Extend(iterable): Merges another list.
Example:
my_list = [1, 2, 3]
my_list.append(4) # [1, 2, 3, 4]
my_list.insert(1, 99) # [1, 99, 2, 3, 4]
another_list = [5, 6]
my_list.extend(another_list) # [1, 99, 2, 3, 4, 5, 6]
List Operations
2. Removing Elements
•remove(value): Removes the first occurrence
•.pop(index): Removes and returns the element at
a given index
•.del list[index]: Deletes an element by index..
Example:
my_list = [1, 99, 2, 3, 4, 5, 6]
my_list.remove(99) # [1, 2, 3, 4, 5, 6]
popped = my_list.pop(2) # Removes 3
print(popped) # Output: 3
del my_list[0] # Deletes the first element
List Indexing and Slicing
Indexing:
• Access elements using their position (index starts at 0).
• Negative indexing starts from the end (-1 refers to the last element).
Example:
my_list = ["a", "b", "c", "d"]
print(my_list[0]) # Output: a
print(my_list[-1]) # Output: d
Slicing:
•Extract parts of a list using list[start:end:step].
•Default start is 0, end is the last element, and step is 1.
Example:
print(my_list[1:3]) # Output: ['b', 'c’]
print(my_list[:2]) # Output: ['a', 'b’]
print(my_list[::-1]) # Reversed list
1. Adding Elements
List Methods
• append(x) → Adds an element x to the end of the list.
• insert(i, x) → Inserts element x at the specified index i.
• extend(iterable) → Appends all elements of an iterable (e.g., another list) to the list.
Example:
lst = [1, 2, 3]
[Link](4) # [1, 2, 3, 4]
[Link](1, 100) # [1, 100, 2, 3, 4]
[Link]([5, 6])
print(lst) # Output: [1, 100, 2, 3, 4, 5, 6]
2. Removing Elements
• remove(x) → Removes the first occurrence of the element x from the list.
• pop(i) → Removes and returns the element at index i (if i is not given, removes the last element).
• clear() → Removes all elements from the list, making it empty.
Example:
[Link](100) # [1, 2, 3, 4, 5, 6]
[Link]() # [1, 2, 3, 4, 5]
[Link]() # []
List Comprehensions: Sorcerer's
Spell Traditional Method:
What is List Comprehension? evens = []
• A shorter way to create lists using a single line of code.
for x in range(10):
• Syntax: [expression for item in iterable if condition] if x % 2 == 0:
[Link](x)
UNDERSTANDING THE SYNTAX OF LIST COMPREHENSION: print(evens)
•expression → The value to be included in the list (can be modified # Output: [0, 2, 4, 6, 8]
before inclusion).
•item → Variable representing elements in the iterable.
•iterable → Any sequence (like list, tuple, string, range, etc.).
•condition (optional) → Filters elements based on a condition.
Using List Comprehension:
evens = [x for x in range(10) if x % 2 == 0]
print(evens) # Output: [0, 2, 4, 6, 8]
Nested Lists
What is a Nested List?
•A list inside another list.
•Useful for representing matrices, tables, or grouped data.
1: Creating a Nested List
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(nested_list) # Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
2: Accessing Elements in a Nested List
print(nested_list[0]) # Output: [1, 2, 3]
print(nested_list[1][2]) # Output: 6
3: Iterating Through a Nested List
for row in nested_list:
for item in row:
print(item, end=“ ”) # Output: 1 2 3 4 5 6 7 8 9
Thank You