Lists in Python
✅ What is a List?
A list is a collection of items in Python. You can think of it like a basket that holds multiple values—like numbers, words, or both.
A list:
- Is ordered (items have a specific position)
- Is changeable (you can add, remove, or update items)
- Allows duplicate values
๐ข How to Create a List
Use square brackets [ ] to create a list:
students = ["ansh", "aarav", "mishti"]
numbers = [1, 2, 3, 4]
mixed = [1, "hello", 3.5, True]
You can also create an empty list:
empty_list = []
๐ข Accessing Items in a List
You can use indexing to access items in a list. Python indexes start from 0.
students = ["ansh", "aarav", "mishti"]
print(students[0]) # ansh
print(students[1]) # aarav
You can also use negative indexing:
print(students[-1]) # mishti (last item)
✏️ Changing List Items
Lists are mutable, meaning you can change values.
fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"
print(fruits) # ['apple', 'mango', 'cherry']
➕ Adding Items to a List
append()
– adds at the end:
fruits.append("orange")
insert()
– adds at a specific position:
fruits.insert(1, "grape") # inserts at index 1
❌ Removing Items from a List
remove()
– removes by value:
fruits.remove("banana")
pop()
– removes by index (default is last item):
fruits.pop() # removes last
fruits.pop(0) # removes first
clear()
– removes all items:
fruits.clear()
๐ Looping Through a List
You can use a for loop:
for fruit in fruits:
print(fruit)
๐ Getting Length of a List
Use the len()
function:
fruits = ["apple", "banana", "cherry"]
print(len(fruits)) # 3
๐ Joining Two Lists
Use the + operator to combine two lists:
list1 = [1, 2]
list2 = [3, 4]
combined = list1 + list2 # [1, 2, 3, 4]
๐ Checking for an Item
Use the in
keyword:
if "apple" in fruits:
print("Yes, apple is in the list")
๐งฉ List with Different Data Types
A list can have different types of data:
my_list = [10, "hello", True, 3.5]
๐งฑ Nested Lists
A list can contain another list (called a nested list):
nested = [1, 2, [3, 4]]
print(nested[2]) # [3, 4]
print(nested[2][1]) # 4
๐งช List Methods (Commonly Used)
Method | Description |
---|---|
append() | Adds item at the end |
insert() | Adds item at specific index |
remove() | Removes item by value |
pop() | Removes item by index |
clear() | Removes all items |
sort() | Sorts the list (ascending by default) |
reverse() | Reverses the list |
index() | Returns index of first match |
count() | Returns number of times item appears |
๐ Sorting a List
numbers = [3, 1, 4, 2]
numbers.sort() # [1, 2, 3, 4]
numbers.sort(reverse=True) # [4, 3, 2, 1]
๐ Copying a List
list1 = [1, 2, 3]
list2 = list1.copy()
๐งฐ Converting List to Other Types
List to Tuple:
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
✅ Summary
- Lists are used to store multiple items in a single variable
- Lists are ordered and changeable
- You can add, remove, and update items
- Lists support looping, sorting, nesting, and more
- Lists are one of the most commonly used data structures in Python
No comments:
Post a Comment