🧩 Python Lists 

🔹 1. What is a List?

A list is an ordered, mutable (changeable) collection of items.
It can store different data types — integers, strings, floats, even other lists!

Python Lists: Comprehensive Guide

✅ Example:

fruits = ["apple", "banana", "cherry"]
numbers = [10, 20, 30, 40]
mixed = [10, "Sakshi", 3.5, True]
 

🔹 2. Creating a List

✅ Various ways:

list1 = []                      # empty list
list2 = list((1, 2, 3))         # from tuple
list3 = ["Python", "Java", "C++"]
 

🔹 3. Accessing Elements (Indexing)

Lists use zero-based indexing.

✅ Example:

fruits = ["apple", "banana", "cherry"]
print(fruits[0])   # apple
print(fruits[-1])  # cherry
 

🔹 4. List Slicing

✅ Example:

nums = [10, 20, 30, 40, 50]
print(nums[1:4])   # [20, 30, 40]
print(nums[:3])    # [10, 20, 30]
print(nums[::-1])  # [50, 40, 30, 20, 10]
 

🔹 5. Modifying a List (Mutable)

✅ Example:

fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"
print(fruits)  # ['apple', 'mango', 'cherry']
 

🔹 6. Adding Elements

MethodDescriptionExample
append()Add single element at endfruits.append("grape")
insert(index, item)Insert at specific positionfruits.insert(1, "orange")
extend()Add multiple itemsfruits.extend(["kiwi", "melon"])

✅ Example:

numbers = [1, 2, 3]
numbers.append(4)
numbers.insert(1, 10)
numbers.extend([5, 6])
print(numbers)  # [1, 10, 2, 3, 4, 5, 6]
 

🔹 7. Removing Elements

MethodDescriptionExample
remove(value)Removes first occurrencefruits.remove("apple")
pop(index)Removes and returns itemfruits.pop(2)
clear()Empties the listfruits.clear()
delDelete using index or entire listdel fruits[0]

✅ Example:

nums = [10, 20, 30, 40]
nums.pop(2)     # removes 30
nums.remove(10) # removes 10
print(nums)     # [20, 40]
 

🔹 8. Looping Through Lists

✅ Example:

colors = ["red", "green", "blue"]
for c in colors:
   print(c)
 

🔹 9. Checking Existence

✅ Example:

names = ["Sakshi", "Riya", "Aman"]
if "Sakshi" in names:
   print("Found!")
 

🔹 10. List Functions

FunctionDescriptionExample
len()Length of listlen(nums)
min()Minimum valuemin(nums)
max()Maximum valuemax(nums)
sum()Sum of all elementssum(nums)
sorted()Returns sorted listsorted(nums)

✅ Example:

nums = [5, 2, 9, 1]
print(len(nums))     # 4
print(sum(nums))     # 17
print(sorted(nums))  # [1, 2, 5, 9]
 

Unraveling the Power of Python Lists | by Aadit Gupta | Medium

🔹 11. List Methods

MethodDescription
count(x)Counts occurrences of x
index(x)Returns index of x
reverse()Reverses list
sort()Sorts list (ascending by default)
copy()Copies list

✅ Example:

nums = [3, 1, 4, 2]
nums.sort()
nums.reverse()
print(nums)  # [4, 3, 2, 1]
 

🔹 12. Nested Lists

✅ Example:

matrix = [
   [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]
]
print(matrix[1][2])  # 6
 

🔹 13. List Comprehension (Shortcut for loops)

✅ Example:

squares = [x**2 for x in range(1, 6)]
print(squares)  # [1, 4, 9, 16, 25]
 

✅ With condition:

even = [x for x in range(10) if x % 2 == 0]
print(even)  # [0, 2, 4, 6, 8]
 

🧠 Mini Practice Programs

  1. Find the largest and smallest number in a list.
  2. Count even and odd numbers from a list.
  3. Remove duplicates from a list.
  4. Create a list of squares of first 10 natural numbers.
  5. Make a list of names and print only those with more than 5 letters.