1. Introduction to Lists in Python

In Python, a list can be defined as a collection of values or items. The items in the list are separated with a comma (,) and enclosed within square brackets [].

Python Code - Empty List
L1 = []
print(L1)
Output
[]
Python Code - Mixed Data Types
L2 = [123, "python", 3.7]
print(L2)
Output
[123, 'python', 3.7]
Python Code - Numbers
L3 = [1, 2, 3, 4, 5, 6]
print(L3)
Output
[1, 2, 3, 4, 5, 6]
Python Code - Strings
L4 = ["C", "Java", "Python"]
print(L4)
Output
['C', 'Java', 'Python']

2. List Indexing

Like string sequences, the indexing of Python lists starts from 0. The first element of the list is stored at the 0th index. Elements can be accessed using the slice operator [].

Python Code - Forward Indexing (0th Element)
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[0])
Output
banana
Python Code - Forward Indexing (2nd Element)
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[2])
Output
mango
Python Code - Forward Slicing
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[1:3])
Output
['apple', 'mango']

Negative Indexing

Unlike other languages, Python provides the flexibility to use negative indexing. The negative indices are counted from the right.

Python Code - Negative Indexing (-1)
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[-1])
Output
berry
Python Code - Negative Indexing (-3)
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[-3])
Output
mango
Python Code - Negative Slicing
mylist = ['banana', 'apple', 'mango', 'tomato', 'berry']
print(mylist[-4:-2])
Output
['apple', 'mango']

3. List Operators

Python Code - Concatenation (+)
num = [1, 2, 3, 4, 5]
lang = ['python', 'c', 'java', 'php']

print(num + lang)
Output
[1, 2, 3, 4, 5, 'python', 'c', 'java', 'php']
Python Code - Repetition (*)
num = [1, 2, 3, 4, 5]

print(num * 2)
Output
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Python Code - Membership (in)
lang = ['python', 'c', 'java', 'php']

print('cpp' in lang)
Output
False
Python Code - Membership (not in)
num = [1, 2, 3, 4, 5]

print(6 not in num)
Output
True

🧠 Pop Quiz 1: Creation & Indexing

Question: If fruits = ["apple", "banana", "cherry", "date"], what will print(fruits[-3:-1]) output?

Click here to reveal the answer

Answer: ['banana', 'cherry']

Explanation: Negative indexing counts from the right. "date" is -1, "cherry" is -2, "banana" is -3. The slice goes from -3 up to (but not including) -1.

4. Updating and Deleting Elements

Updating Elements

Python allows us to modify the list items by using the assignment operator (=).

Python Code - Updating an Item
num = [1, 2, 3, 4, 5]
num[2] = 30

print(num)
Output
[1, 2, 30, 4, 5]
Python Code - Updating a Range of Items
num = [1, 2, 30, 4, 5]
num[1:3] = [25, 36]

print(num)
Output
[1, 25, 36, 4, 5]

Deleting Elements

Python allows us to delete one or more items in a list by using the del keyword.

Python Code - Deleting an Item
num = [1, 2, 3, 4, 5]
del num[1]

print(num)
Output
[1, 3, 4, 5]
Python Code - Deleting a Range of Items
num = [1, 3, 4, 5]
del num[1:3]

print(num)
Output
[1, 5]

5. Iterating a List

A list can be iterated by using a for in loop.

Python Code - Iterating
lang = ['python', 'c', 'java', 'php']
print("The list items are \n")

for i in lang:
    print(i)
Output
The list items are python c java php

🧠 Pop Quiz 2: Modification

Question: How would you permanently remove the first element of a list named data?

Click here to reveal the answer

Answer: del data[0]

6. List Functions & Methods

Python Code - len()
num = [1, 2, 3, 4, 5, 6]

print("length of list :", len(num))
Output
length of list : 6
Python Code - max()
list1 = [1, 2, 3, 4, 5, 6]

print("Max of list1 :", max(list1))
Output
Max of list1 : 6
Python Code - min()
list2 = ['java', 'c', 'python', 'cpp']

print("Min of list2 :", min(list2))
Output
Min of list2 : c
Python Code - sum()
list1 = [1, 2, 3, 4, 5, 6]

print("Sum of list items :", sum(list1))
Output
Sum of list items : 21
Python Code - sorted() function
num = [1, 3, 2, 4, 6, 5]

print(sorted(num))
Output
[1, 2, 3, 4, 5, 6]
Python Code - sort() method
list1 = [6, 8, 2, 4, 10]
list1.sort(reverse=True)

print(list1)
Output
[10, 8, 6, 4, 2]
Python Code - append()
num = [1, 2, 3, 4, 5]
num.append(6)

print(num)
Output
[1, 2, 3, 4, 5, 6]
Python Code - insert()
num = [10, 20, 30, 40, 50]
num.insert(4, 60)

print(num)
Output
[10, 20, 30, 40, 60, 50]
Python Code - remove()
list1 = [1, 2, 3, 4, 5]
list1.remove(2)

print(list1)
Output
[1, 3, 4, 5]
Python Code - pop()
num = [10, 20, 30, 40, 50]
num.pop()

print(num)
Output
[10, 20, 30, 40]
Python Code - clear()
num = [10, 20, 30, 40, 50]
num.clear()

print(num)
Output
[]
Python Code - count()
num = [1, 2, 3, 4, 3, 2, 2, 1, 4, 5, 8]
cnt = num.count(2)

print("Count of 2 is:", cnt)
Output
Count of 2 is: 3
Python Code - index()
list1 = ['p', 'y', 't', 'o', 'n', 'p']

print(list1.index('t'))
Output
2
Python Code - reverse()
list1 = [6, 8, 2, 4, 10]
list1.reverse()

print(list1)
Output
[10, 4, 2, 8, 6]

🧠 Pop Quiz 3: Functions and Methods

Question: What is the primary difference between remove() and pop()?

Click here to reveal the answer

Answer: remove() deletes an element by specifying its value (e.g., remove("apple")) and removes the first occurrence. pop() deletes an element by specifying its index (e.g., pop(1)) and returns the removed element.