Python Lists

🐍 Python Lists — Full Tutorial

A list in Python is an ordered, changeable (mutable) collection that can store multiple items of different data types.

Lists are written using square brackets []:

my_list = ["Apple", "Banana", "Cherry"]

🔹 1. Creating a List

numbers = [1, 2, 3, 4, 5]
mixed = [10, "Hello", True, 5.5]
empty_list = []

🔹 2. Accessing List Items (Indexing)

Indexes start from 0:

Apple Banana Cherry
0 1 2

fruits = ["Apple", "Banana", "Cherry"]

print(fruits[0]) # Apple
print(fruits[2]) # Cherry

Negative indexing:

print(fruits[-1]) # Cherry
print(fruits[-2]) # Banana

🔹 3. Slicing Lists

Extract a portion of the list:

fruits = ["Apple", "Banana", "Cherry", "Mango", "Orange"]

print(fruits[1:4]) # [‘Banana’, ‘Cherry’, ‘Mango’]
print(fruits[:3]) # [‘Apple’, ‘Banana’, ‘Cherry’]
print(fruits[2:]) # [‘Cherry’, ‘Mango’, ‘Orange’]


🔹 4. Modifying List Items

Lists are mutable, meaning you can change values.

fruits = ["Apple", "Banana", "Cherry"]
fruits[1] = "Kiwi"
print(fruits) # [‘Apple’, ‘Kiwi’, ‘Cherry’]


🔹 5. Adding Items to a List

Method Action
append() Adds item at end
insert() Adds item at specific index
extend() Adds multiple items

Examples:

fruits = ["Apple", "Banana"]

fruits.append(“Orange”) # Add at end
fruits.insert(1, “Kiwi”) # Add at position
fruits.extend([“Mango”, “Grapes”]) # Add multiple

print(fruits)


🔹 6. Removing Items

Method Action
remove() remove by value
pop() remove by index (default removes last item)
del delete using index or whole list
clear() empty the list

Examples:

list1 = ["Apple", "Banana", "Cherry"]

list1.remove(“Banana”)
list1.pop(0)
del list1[0]
list1.clear()


🔹 7. Looping Through a List

fruits = ["Apple", "Banana", "Cherry"]

for x in fruits:
print(x)

With index:

for i in range(len(fruits)):
print(i, fruits[i])

🔹 8. Checking if Item Exists

fruits = ["Apple", "Banana"]

print(“Apple” in fruits) # True
print(“Kiwi” not in fruits) # True


🔹 9. Sorting Lists

numbers = [5, 3, 9, 1]
numbers.sort() # ascending
numbers.sort(reverse=True) # descending
print(numbers)

Sorting strings:

fruits = ["Banana", "Apple", "Cherry"]
fruits.sort()
print(fruits)


🔹 10. Copying Lists (Important)

list1 = [1, 2, 3]
list2 = list1.copy()
print(list2)

or:

list2 = list1[:]

🔹 11. Joining Lists

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2

or:

list1.extend(list2)

🔹 12. Built-in List Functions

Function Purpose
len() count items
max() largest value
min() smallest value
sum() sum of numbers

Example:

numbers = [10, 20, 30]
print(len(numbers)) # 3
print(max(numbers)) # 30
print(min(numbers)) # 10
print(sum(numbers)) # 60

🔹 13. List Comprehension (Advanced but important)

A short way to create lists:

numbers = [x for x in range(10)]
print(numbers)

Filter example:

even = [x for x in range(10) if x % 2 == 0]
print(even)

⭐ Mini Project Example

tasks = []

while True:
task = input(“Enter task (or ‘exit’): “)

if task == “exit”:
break
tasks.append(task)

print(“\nYour tasks:”)
for t in tasks:
print(“-“, t)

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *