Lists in Python
1. Introduction to Lists
• A list is a built-in data structure in Python that can store multiple
values of different types.
• Lists are mutable, meaning they can be modified after creation.
• They are enclosed within square brackets [ ] and elements are
separated by commas ,.
Examples:
# Empty list
list1 = []
# List of integers
list2 = [1, 2, 3, 4, 5]
# List of floating-point numbers
list3 = [1.1, 2.2, 3.3]
# List of strings
list4 = ["apple", "banana", "cherry"]
# List with mixed data types
list5 = ["Hello", 42, 3.14, True]
# Nested list (List inside another list)
list6 = ["One", [2, 4, 6], ["A", "B", "C"]]
2. Creating Lists
Lists can be created using:
• Direct Assignment
• Using list() Constructor
• List Comprehension
Examples:
# Using list() constructor
list1 = list((1, 2, 3))
print(list1) # Output: [1, 2, 3]
# Using list comprehension
squares = [x**2 for x in range(1, 6)]
print(squares) # Output: [1, 4, 9, 16, 25]
3. Accessing Elements
List elements are accessed using indexing.
Positive Indexing (Left to Right)
list1 = ["Python", "Java", "C++", "JavaScript"]
print(list1[0]) # Output: "Python"
print(list1[2]) # Output: "C++"
Negative Indexing (Right to Left)
print(list1[-1]) # Output: "JavaScript"
print(list1[-3]) # Output: "Java"
4. Modifying Lists
Lists are mutable, meaning elements can be modified.
Changing an Element
list1[1] = "C#"
print(list1) # Output: ['Python', 'C#', 'C++', 'JavaScript']
Appending Elements (.append())
list1.append("Ruby")
print(list1) # Output: ['Python', 'C#', 'C++', 'JavaScript', 'Ruby']
Inserting Elements (.insert())
list1.insert(2, "Swift")
print(list1) # Output: ['Python', 'C#', 'Swift', 'C++', 'JavaScript', 'Ruby']
5. List Operations
Concatenation (+)
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list1 + list2
print(result) # Output: [1, 2, 3, 4, 5, 6]
Replication (*)
list1 = [0, 1]
print(list1 * 3) # Output: [0, 1, 0, 1, 0, 1]
6. List Slicing
numbers = [10, 20, 30, 40, 50, 60, 70, 80]
print(numbers[2:5]) # Output: [30, 40, 50]
print(numbers[:4]) # Output: [10, 20, 30, 40]
print(numbers[3:]) # Output: [40, 50, 60, 70, 80]
print(numbers[::2]) # Output: [10, 30, 50, 70]
print(numbers[::-1]) # Output: [80, 70, 60, 50, 40, 30, 20, 10]
7. List Methods
8. Two-Dimensional Lists (2D Lists)
A 2D list is a list of lists, like a matrix.
Creating a 2D List
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Accessing Elements
print(matrix[0]) # Output: [1, 2, 3]
print(matrix[1][2]) # Output: 6
Modifying Elements
matrix[1][1] = 10
print(matrix) # Output: [[1, 2, 3], [4, 10, 6], [7, 8, 9]]
9. Iterating Over Lists
Using for loop
for row in matrix:
for col in row:
print(col, end=" ")
print()
Using List Comprehension
flattened = [col for row in matrix for col in row]
print(flattened) # Output: [1, 2, 3, 4, 10, 6, 7, 8, 9]
11. Taking User Input for Lists
Single Line Input
lst = list(map(int, input().split()))
print(lst)
Multi-line Input (2D List)
rows = int(input("Enter rows: "))
cols = int(input("Enter cols: "))
matrix = [[int(input()) for _ in range(cols)] for _ in range(rows)]
print(matrix)
*Note
Lists are ordered, mutable, and can store multiple data types.
Operations include concatenation (+), replication (*), slicing, and looping.
List methods like append(), pop(), remove(), and sort() help manage elements.
2D Lists store tabular data and are accessed using two indices.
List comprehensions provide a compact way to create lists.
Searching can be done using linear search.
User inputs can be taken in space-separated or multi-line format.