What is a List?
- A Python list is the simple collection of values the represented in a particular list inside square brackets.
- A list can store any type of data, such as numbers, words, Booleans, or even another list. Because lists are flexible and easy to modify in our program.
- Imagine, list are the shopping basket, you can put multiple items inside it, remove items, or change them whenever you need.
Syntax of List In Python
list_name = [element1, element2, element3, ...]
- See the syntax carefully; items are separated by commas
- The order of elements is preserved
- You can access and update items easily
Further, we will discuss how lists are useful in our real-life projects.
Example 1 – List of Even Numbers
even_numbers = [2, 4, 6, 8, 10]
print(even_numbers)
Example 2 – List of Cities
cities = ["Delhi", "Tokyo", "Berlin", "Sydney"]
print(cities)
Example 3 – A List Containing Mixed Data Types
profile = ["Arun", 27, 5.8, False]
print(profile)
- This list includes a string, an integer, a floating-point number, and a Boolean.
Example 4 – A List That Stores Product Information
product_details = ["Laptop", 59999, 1.4, ["Black", "Silver"]]
print(product_details)
- Here, we have created two lists, which means the last element itself is another list.
Characteristics of Lists In Python
Python provides some powerful features for lists that make them flexible.
1) Ordered: This characteristic refers that lists are stored in a specific sequence. For example:
colors = ["red", "green", "blue"]
print(colors)
# Output will always keep this order
2) Mutable (It can be modified): We can easily change, update an item, add new values, or delete existing items from list elements after they are created.
Example of Mutable List:
scores = [45, 50, 60]
scores[1] = 55 # Updating the second element
print(scores)
Output of the edited list:
[45, 55, 60]
3) Allow Duplicates: A list doesn’t restrict you from adding the same value more than once. This is helpful when data are naturally repeated and added to our list.
visitors = ["Amit", "Riya", "Amit", "Kunal"]
print(visitors)
- You can see the “Amit” name is repeated twice.
How To Access a List Elements In Python?
You can access list items using their index function, because the index tells Python the exact position of the element you want.
Remember one thing: the list always starts from 0, not 1. It means the first item is at position 0, and the second at 1.
Syntax of Index In List:
list_name[index]
Example:
cities = ["Delhi", "Mumbai", "Kolkata", "Chennai"]
print(cities[0]) # Output: Delhi (first item)
print(cities[2]) # Output: Kolkata (third item)
- This indexing method is useful when you know exact position of values.
Accessing Elements Using Negative Indexes
- Negative indexes also count from the right side, and start from -1. For example:
cities = ["Delhi", "Mumbai", "Kolkata", "Chennai"]
print(cities[-1]) # Output: Chennai (last item)
print(cities[-2]) # Output: Kolkata (second last)
Quick Tip: If you try to access an index that does not exist (like languages[10], Python will raise an IndexError.
What Is Slicing In Lists
List slicing allows us to extract or find out the specific portion (slice) of a list without modifying the original one.
Syntax of Slicing:
list_name[start:end:step]
- start: index where slicing begins.
- end: index where slicing stops.
- step: jump size between elements.
Example of Slicing:
Example 1: Basic Slice
marks = [55, 60, 72, 81, 90, 95]
top_three = marks[1:4]
print(top_three)
# Output: [60, 72, 81]
- In the above code, indexing starts at index 1 (60) and ends before index 4.
Example 2: Slice With Step Size
marks = [55, 60, 72, 81, 90, 95]
alternate_scores = marks[0:6:2]
print(alternate_scores)
# Output: [55, 72, 90]
- Here we took every second value using step 2.
Example 3: Reverse the List Using Slicing
marks = [55, 60, 72, 81, 90, 95]
reverse_order = marks[::-1]
print(reverse_order)
# Output: [95, 90, 81, 72, 60, 55]
- Negative step automatically reverses the list.
Example 4: Slice Without Giving Start or End
movies = ["Inception", "Dune", "Avatar", "Interstellar", "Tenet"]
first_two = movies[:2] # start missing → begins at 0
last_two = movies[-2:] # end missing → goes till last
middle_part = movies[1:-1] # exclude first & last
print(first_two) # ['Inception', 'Dune']
print(last_two) # ['Interstellar', 'Tenet']
print(middle_part) # ['Dune', 'Avatar', 'Interstellar']
Common List Operations In Python
Python allows you to add, remove, update, and perform many operations anytime using their important pre-built keywords. Let’s explore each:
1. Adding Elements to a List
Python provides three best methods to add items into a list.
- append(): Add only single element at the end of the list
- insert(): Add an element at a specific position
- extend(): Add multiple elements at once
Simple Example:
cart = ["notebook", "pen"]
# Add an item to the end
cart.append("eraser")
print(cart)
### Output: ['notebook', 'pen', 'eraser']
# Insert item at index 1
cart.insert(1, "marker")
print(cart)
### Output: ['notebook', 'marker', 'pen', 'eraser']
# Add multiple stationery items
cart.extend(["scale", "sharpener"])
print(cart)
### Output: ['notebook', 'marker', 'pen', 'eraser', 'scale', 'sharpener']
2. Removing Elements From a List
We can remove items using three best methods:
- remove(): Removes the first occurrence of a value.
- pop(): Removes an element by index (default: last).
- clear(): Removes all elements easily.
Example of remove elements:
tasks = ["wake up", "exercise", "study"]
# Remove a specific task by its name
tasks.remove("exercise")
print(tasks)
# Output: ['wake up', 'study']
# Remove the first task (index 0)
tasks.pop(0)
print(tasks)
# Output: ['study']
# Clear the entire task list
tasks.clear()
print(tasks)
# Output: []
All The List Methods
| Method | Description |
|---|---|
| append() | Adds an element to the end of the list. |
| insert() | Inserts an element at a specified position. |
| extend() | Adds multiple elements to the end of the list. |
| remove() | Removes the first occurrence of a specified element. |
| pop() | Removes an element by index or the last element. |
| clear() | Clears all elements from the list. |
| index() | Returns the index of the first occurrence of a value. |
| count() | Counts the occurrences of a specified element. |
| sort() | Sorts the list in ascending or descending order. |
| reverse() | Reverses the order of the list. |
| copy() | Returns a shallow copy of the list. |
Iterating Through a List in Python
Iterating means we are going through each item in a list one by one, and Python makes this easy using a for loop.
Example:
submitted = ["Arjun", "Meera", "Ravi"]
for name in submitted:
print("Received from:", name)
Output:
Received from: Arjun
Received from: Meera
Received from: Ravi
List Comprehensions
List comprehensions provide a concise way to create lists.
Syntax:
new_list = [expression for item in iterable if condition]
Example 1:
# Create a list of squares
squares = [x**2 for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]
# Filter even numbers
even_numbers = [x for x in range(10) if x % 2 == 0]
print(even_numbers) # Output: [0, 2, 4, 6, 8]
Example 2:
products = ["soap", "shampoo", "sugar", "brush", "salt"]
# Take only items starting with 's'
s_items = [item for item in products if item.startswith("s")]
print(s_items)
Output:
['soap', 'shampoo', 'sugar', 'salt']
- What is type casting in Python?
- What are numbers in Python?
- How we can use data types in Python?
- What are variables in Python?
- What are strings in Python?
Nested Lists In Python
A nested list means a list stored inside another list. This method allows us to organize data in rows and columns, similar to a table or grid.
Example of Nested List:
Let’s understand how to represent a student’s marks using a nested list.
marks = [
[85, 90, 88], # Student 1 → Maths, Science, English
[78, 82, 80], # Student 2
[92, 89, 95] # Student 3
]
# Accessing the full record of Student 1
print("Student 1 marks:", marks[0])
# Accessing English marks of Student 2 (3rd subject)
print("Student 2 English mark:", marks[1][2])
Output:
Student 1 marks: [85, 90, 88]
Student 2 English mark: 80

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.