PythonLists

LIST

A list in Python is an ordered, mutable sequence that can contain elements of various data types, such as integers, floats, strings, and even other lists or tuples. Lists are highly versatile and are defined using square brackets [], with elements separated by commas.


Examples of Lists

# A List containing numbers (int)
list1 = [1, 2, 3, 4, 5]
print(list1)  # Output: [1, 2, 3, 4, 5]
 
# A List containing alphabets (string)
list2 = ['a', 'b', 'c', 'd', 'e']
print(list2)  # Output: ['a', 'b', 'c', 'd', 'e']
 
# A List containing mixed data types
list3 = ['a', 1, 'b', 2, 'c', 3]
print(list3)  # Output: ['a', 1, 'b', 2, 'c', 3]
 
# A Nested List with mixed data types
list4 = [['a', 1], ['b', 2], ['c', 3]]
print(list4)  # Output: [['a', 1], ['b', 2], ['c', 3]]

Accessing Elements in a List

Each element in a list can be accessed using its index. Indexing in Python starts from 0 for the first element. Negative indexing can also be used to access elements from the end of the list.

list1 = [1, 3, 5, 7, 9, 11]
 
# Accessing elements using positive indexing
print(list1[0])  # Output: 1
print(list1[3])  # Output: 7
 
# Accessing elements using negative indexing
print(list1[-1])  # Output: 11
 
# Length of the list
n = len(list1)
print(n)  # Output: 6
 
# Get the last element
print(list1[n-1])  # Output: 11
print(list1[-n])  # Output: 1
 
# Expression as an index
print(list1[1 + 4])  # Output: 11

Note: Attempting to access an out-of-range index will raise an IndexError.


Mutability of Lists

Lists are mutable, meaning their elements can be modified after creation.

# Create a List
list1 = ['Football', 'Basketball', 'Cricket', 'Tennis']
 
# Modify an element
list1[1] = 'Handball'
 
# Print the updated list
print(list1)  # Output: ['Football', 'Handball', 'Cricket', 'Tennis']

List Operations

1. Concatenation

Concatenation joins two or more lists into one.

list1 = ['Football', 'Basketball']
list2 = [1, 2, 3, 4]
 
list3 = list1 + list2
print(list3)  # Output: ['Football', 'Basketball', 1, 2, 3, 4]

Note: Only lists can be concatenated; attempting to concatenate a list with a non-list will raise an error.


2. Repetition/Replication

Replication repeats a list n times using the * operator.

list1 = ['Hey', 'Hi']
 
# Replicate the list
print(list1 * 3)  # Output: ['Hey', 'Hi', 'Hey', 'Hi', 'Hey', 'Hi']

3. Membership

Membership operators in and not in check for the presence of an element in a list.

list1 = ['Football', 'Basketball', 'Cricket', 'Tennis']
 
print('Cricket' in list1)  # Output: True
print('Squash' not in list1)  # Output: True

4. Slicing

Slicing extracts a range of elements from a list.

list1 = ['Football', 'Basketball', 'Cricket', 'Tennis', 'Kho Kho', 'Volleyball', 'Skating', 'Badminton']
 
# Extract a slice using positive indexing
print(list1[3:6])  # Output: ['Tennis', 'Kho Kho', 'Volleyball']
 
# Slicing with a step value
print(list1[1:6:2])  # Output: ['Basketball', 'Tennis', 'Volleyball']
 
# Slicing using negative indexing
print(list1[-5:-2])  # Output: ['Tennis', 'Kho Kho', 'Volleyball']
 
# Reverse the list using slicing
print(list1[-3:-7:-1])  # Output: ['Volleyball', 'Kho Kho', 'Tennis', 'Cricket']

Key Notes:

  • Lists are mutable, meaning their contents can be modified.
  • List indexing allows both positive and negative indices.
  • Operations like concatenation, repetition, and slicing make lists versatile.

Practice Problems

Problem 1: Create a List

Create a list of your 5 favorite movies and print them using a for loop.

Show Code
movies = ['Inception', 'Titanic', 'Avatar', 'The Matrix', 'Gladiator']
 
for movie in movies:
    print(movie)

Problem 2: Modify a List

Create a list of 5 fruits. Replace the third fruit with your favorite fruit.

Show Code
fruits = ['Apple', 'Banana', 'Mango', 'Grapes', 'Orange']
fruits[2] = 'Pineapple'
 
print(fruits)  # Output: ['Apple', 'Banana', 'Pineapple', 'Grapes', 'Orange']

Problem 3: Slicing

Create a list of 10 numbers and display the numbers from index 2 to 6 in reverse order.

Show Code
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
print(numbers[6:1:-1])  # Output: [7, 6, 5, 4, 3]

Pyground

Play with Python!

Output: