Python Lists

← Back to Home

Python Lists Explained with Examples – A Beginner to Pro Guide


Python is known for its simplicity and powerful built-in data structures. One of the most important and widely used structures in Python is the list.

Whether you're a student just starting out or a professional working on a project, understanding Python lists can help you write cleaner, faster, and more efficient code.



✅ What is a List in Python?

A list in Python is a collection of items that are ordered, changeable (mutable), and allow duplicate values.

You can store:

  • Numbers
  • Strings
  • Other lists
  • Even a mix of different data types!


Creating a List

# Creating different types of lists
numbers = [1, 2, 3, 4, 5]
names = ["Alice", "Bob", "Charlie"]
mixed = [10, "hello", 3.14, True]


📌 Basic List Operations

1. Accessing Items

Lists use indexing starting from 0.

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

2. Changing Items

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

3. Adding Items

fruits.append("orange")         # Add at end
fruits.insert(1, "mango")       # Add at specific position

4. Removing Items

fruits.remove("apple")          # Remove by value
fruits.pop()                    # Remove last item
del fruits[0]                   # Remove by index

5. List Length

print(len(fruits))  # Number of items


🔄 Looping Through a List

for fruit in fruits:
    print(fruit)


Useful List Methods

Method Description
append() Adds an item to the end
insert() Inserts item at a specific index
remove() Removes the first occurrence
pop() Removes item at index or last
sort() Sorts the list (in place)
reverse() Reverses the order of items
clear() Removes all items
index() Returns index of first occurrence
count() Counts number of occurrences

Example:

scores = [45, 87, 45, 92, 60]
print(scores.count(45))    # Output: 2
print(scores.index(92))    # Output: 3


Advanced List Tricks (Useful for Professionals)

1. List Comprehension

A concise way to create lists.

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

2. Slicing

Extract parts of a list.

nums = [0, 1, 2, 3, 4, 5]
print(nums[1:4])    # [1, 2, 3]
print(nums[:3])     # [0, 1, 2]
print(nums[-2:])    # [4, 5]

3. Nested Lists

Lists inside lists (like a matrix).

matrix = [
    [1, 2],
    [3, 4],
    [5, 6]
]
print(matrix[1][1])  # Output: 4


When to Use Lists?

  • Storing ordered data like names, numbers, tasks
  • Looping through a group of values
  • Performing bulk operations (filter, sort, etc.)
  • Building custom data structures like stacks/queues


📝 Quick Summary

  • Lists are ordered, mutable collections.
  • You can add, remove, change, or access items easily.
  • Great for storing groups of related data.
  • Widely used in Python for data manipulation, web development, automation, and more.


đź’ˇ Practice Challenge

Try this:

# Create a list of 5 favorite movies
# Print only the 2nd and 4th movie from the list

movies = ["Inception", "Interstellar", "The Matrix", "The Dark Knight", "Tenet"]
print(movies[1])
print(movies[3])


🎓 Conclusion

Python lists are a core part of the language that you'll use in almost every project. They're simple enough for beginners and powerful enough for professionals.

Whether you're building a to-do app or crunching big data, knowing how to use lists efficiently is a skill every Python developer needs.




The above video describes python lists in a very easy and smooth manner for the viewers who want to learn it .Watch it with examples and hands on exercises.
Learn the concept of lists in python and get answers of the following questions:

What is a python list ?
How to create lists in python?
Populating list with items.
How to access items from a list in python?
Slicing in python.
Concatenation of two lists in python.