Table of Content
- What is a List ?
- How to Create Python List?
- How to Access Elements in a list?
- How to Add Elements from a list?
- How to Remove Elements from a list?
- List Operations
- List Methods
- Looping Through Lists
- List compression
- Mini Projects
- Common Mistakes & Fixes
1. What is a List ?
A list is an ordered, changeable (mutable) collection of items which can store multiple data types.
⭐ It is separated by commas and enclosed in square bracket [ ]
✅ Importance of List:
- Store multiple values in one variable
- Allow easy access and modification of data
- Support dynamic addition and removal of elements
- Widely used for efficient data handling in Python
2. How to Create Python List?
Empty List: List with no item.
Example: empty_list = []
List with numbers / float / strings / mixed:
Example: list = [10, "YBI", 3.14, False]
Nested List: A list that contains another list as its element.
Example: nested_list = [[1, 2], [3, 4]]
▶️ Video Tutorial : https://youtu.be/4Vodq9yx5S0
3. How to Access Elements in a list?
Let’s consider an example list:
my_list = [“apple”, 1, “berries”, 10]
Methods:
Indexing — Accessing a single element
A). Positive Indexing:
#To access its First Element (index 0)
print(my_list[0])
⛳ Output:
apple
B). Negative Indexing:
#To access elements from last
print(my_list[-1])
⛳ Output:
10
- Slicing — Access a group of elements .
#Syntax
print(my_list[0:2])
⛳ Output:
[apple, 1]
4. How to Add Elements to a list?
Let’s consider an example list:
my_list = [“apple”, 1, “berries”, 10]
Methods:
append() → Add at end
Syntax: list_name.append(item)
Example:
my_list.append(24)
⛳ Output:
[apple, 1, berries, 10, 24]
insert() → Insert at specific index
Syntax: list_name.insert(index, item)
Example:
my_list.insert(1, “mango”)
⛳ Output:
[apple, mango, berries, 10]
extend() → Add multiple items
Syntax: list_name.extend(iterable)
Example:
my_list.extend(60, “banana”)
⛳ Output:
[apple, 1, berries, 10, 60, banana]
▶️ Video Tutorial : https://youtu.be/KocNchGaoBA
5. How to Remove Elements from a list
Let’s consider an example list:
my_list = [“apple”, 1, “berries”, 10]
Methods:
remove() → Remove by value
Syntax: list_name.remove(item)
Example:
my_list.remove(“apple”)
⛳ Output:
[1, berries, 10]
pop() → Remove by index
Syntax: list_name.pop(index)
Example:
⛳ Output:
[apple, 1, 10]
[apple, 1, berries]
clear() → Empty the list
Syntax: list_name.clear()
Example:
my_list.clear()
⛳ Output:
[ ]
del keyword → delete item using keyword
Syntax: del list_name(item)
Example:
del my_list(2)
⛳ Output:
[apple, 1, 10]
6. List Operations
Methods:
Concatenation (+) → Adding two lists.
Syntax: list1 + list2
a = [1. 2]
b = [3, 4]
print(a + b)
⛳ Output:
[1, 2, 3, 4]
Repetition (*) → Repeating list elements multiple times.
Syntax: list * number
Example:
a = [1. 2]
print(a * 3)
⛳ Output:
[1, 2, 1, 2, 1, 2]
Membership (in, not in) → checks if an element exists in a list.
Syntax: item in list
Example:
a = [1. 2]
print(2 in a)
⛳ Output:
True
7. List Methods
Methods:
sort() → Sort list
Syntax: list_name.sort()
Example:
a = [2, 3, 1]
a.sort()
⛳ Output:
[1, 2, 3]
Reverse() →Reverse order of list
Syntax: list_name.reverse()
Example:
a = [1, 2, 3]
a.reverse()
⛳ Output:
[3, 2, 1]
count() → Count occurrences in list
Syntax: list_name.count(item)
Example:
a = [2, 2, 3, 3, 3, 1]
⛳ Output:
2
index() → Find index of item in list
Syntax: list_name.index(item)
Example:
a = [3, 2, 1]
print(a.index(3))
⛳ Output:
1
copy() → Copy list
Syntax: list_name.copy()
Example:
a = [1, 2]
y=a.copy()
print(y)
⛳ Output:
[1, 2]
Minimum - Maximum :
min() → find the smallest item in the list
max() → find the largest item in the list
Example:
a = [5, 2,7, 9]
print(min(a))
print(max(a))
⛳ Output:
2
9
8. Looping Through Lists
Methods:
Using for loop → A for loop directly accesses each element of the list.
Syntax:
for item in list_name:
Example:
fruits = ["apple", "banana", "mango"]
for item in fruits:
print(item)
⛳ Output:
apple
banana
mango
Loop Using Index →Access list elements using their index numbers.
Syntax:
Example:
fruits = ["apple", "banana", "mango"]
⛳ Output:
Loop with While → A while loop runs until a condition becomes false
Syntax:
Example:
⛳ Output:
9. List Compression
It is a Shortcut for Creating Lists.
⭐ Why to Use List compression?
- Shorter code
- Easy to read
- Faster than loops
Examples:
- Create a new List of Squares
squares = [x*x for x in range(1, 6)]
print(squares)
⛳ Output:
[1, 4, 9, 16, 25]
- Create List from Existing List
nums = [1, 2, 3, 4]
double = [x*2 for x in nums]
print(double)
⛳ Output:
[2, 4, 6, 8]
With Condition :
Syntax: new_list = [expression for item in iterable if condition]
Example: Create List from Existing List
even = [x for x in range(1, 11) if x % 2 == 0]
print(even)
⛳ Output:
[2, 4, 6, 8, 10]
10. Mini Projects with Python List
1. Create a new List of Squares
Solution : using append(), insert()
nums = [1, 2, 3]
nums.append(4)
nums.insert(1, 10)
print(nums)
⛳ Output:
[1, 10, 2, 3, 4]
2. Count Frequency of Elements
Solution : count the frequency of items.
data = [1, 2, 2, 3, 3, 3]
for i in set(data):
print(i, ":", data.count(i))
⛳ Output:
1 : 1
2 : 2
3 : 3
11. Common Mistakes and Fixes
1️⃣ Dropping non-existing column → KeyError
❌ Mistake:df.drop(columns=['City'])
✅ Fix:df.drop(columns=['City'], errors='ignore')
2️⃣ Wrong axis when dropping columns
df.drop('Marks')
✅ Fix:df.drop(columns=['Marks']) or df.drop('Marks', axis="1)
3️⃣ Missing parentheses in multiple conditions
❌ Mistake:df[df['Age'] > 20 & df['Marks'] > 85]
✅ Fix:df[(df['Age'] > 20) & (df['Marks'] > 85)]
4️⃣ Using and / or instead of & / |
❌ Mistake:df[(df['Age'] > 20) and (df['Marks'] > 85)]
✅ Fix:df[(df['Age'] > 20) & (df['Marks'] > 85)]
5️⃣ Expecting drop() to modify original DataFrame
❌ Mistake:df[(df['Age'] > 20) and (df['Marks'] > 85)]
✅ Fix:df[(df['Age'] > 20) & (df['Marks'] > 85)]
