Python Lists

← Back to Home

Python Lists Explained with Examples – A Beginner to Pro Guide


Python is popular because it is easy to learn and has useful built-in tools. One of the most common tools is the list.

If you are just starting to learn coding and you're learning Python for the first time or using it for work and working on your first project, understanding how lists work can make your programs easier to write and faster to run.

Knowing how lists work can help you write better and faster code.



✅ What is a List in Python?

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

It means that a list items stay in order. You can change the items anytime, and it's okay to have repeated values.

A list in Python is like a box where you can keep many items. These items stay in the order you put them, and you can change them whenever you want.

You can put many things in a list, like:

  • Numbers
  • Strings
  • Other lists
  • or 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]


Example 1: A simple list of numbers

# Creating list of numbers
numbers = [2, 3, 4, 5, 6]
print(numbers)

Output:
 
  [2, 3, 4, 5, 6]


Example 2: A list of words (strings)

# Creating list of numbers
fruits = ["apple", "banana", "cherry"]
print(fruits)


Output:
 
  ['apple', 'banana', 'cherry']



Example 3: A mixed list

# mixed list of items
mixed = [10, "hello", 3.14, True]
print(mixed)

Output:
 
 [10, 'hello', 3.14, True]



Example 4: A list inside another list

# mixed list of items
nested = [1, [2, 3], "hi"]
print(nested)

Output:
 
 [1, [2, 3], 'hi']




📌 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 an important part of the language, and you'll use them in nearly every project. They're simple for beginners and useful enough for professionals in advanced programs.

Whether you're creating a simple to-do app or crunching big data with large data sets, knowing how to use lists efficiently is a skill every Python developer needs to have.




This 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.