![]() |
| Python list operations example showing append, insert, remove, and sort methods |
A List in Python is a collection used to store multiple items in one variable. Lists are ordered, changeable, and allow duplicates ✅.
📌 Allow Duplicates
my_list = ["cat","dog","cat"]
Here, "cat" appears two times. Python lists allow duplicate values.
📏 Length of a List
print(len(my_list))
len() function shows how many items are inside the list.
📦 List Items Can Be Any Data Type
my_list = ["xyz", 22, True]
A list can store string, integer, boolean, or mixed data types.
🏗 Using list() Constructor
my_list = list(("cat","lion","dog"))
You can create a list using the list() constructor.
🔢 Index Numbers
my_list = ["cat","dog","cat","lion","elephant","rat"] # 0 1 2 3 4 5
Each item has an index number starting from 0.
Access Item Using Index
print(my_list[1]) # dog
📚 Range of Indexes
print(my_list[2:5]) # cat, lion, elephant print(my_list[:3]) # cat, dog, cat print(my_list[2:]) # cat, lion, elephant, rat
Range starts from first index and ends before the last index number.
✏ Change List Items
my_list[1] = "tiger" print(my_list)
my_list[1:3] = ["zebra","tiger"] print(my_list)
➕ Insert Item
my_list.insert(2,"zebra") print(my_list)
📌 Append Item
my_list.append("zebra")
print(my_list)
append() adds item to the end of the list.
🔗 Extend List
your_list = ["1","2"] my_list.extend(your_list) print(my_list)
extend() adds another list to the current list.
❌ Remove Item
my_list.remove("cat")
remove() removes the specified value.
🗑 Pop Item
my_list.pop(1)
pop() removes item using index number.
🗑 Delete Item
del my_list[0]
del keyword deletes item completely.
🔄 Sort List
my_list.sort()
Sorts list in ascending order.
my_list.sort(reverse=True)
Sorts list in descending order.
📋 Copy List
new_list = my_list.copy()
copy() creates a new copy of the list.
➕ Join Two Lists
list1 = ["a","b"] list2 = [1,2,3] list3 = list1 + list2 print(list3)
Using + operator we can combine two lists.
🎯 Python lists are powerful and flexible. They are widely used in Data Science, AI, and Web Development 🚀.
