Yashuv Baskota
4 min readDec 30, 2022

The Power of Python Lists: 10 Examples to Help You Get Started 🔥

- Master Python Lists

Image by Yashuv Baskota

In Python, a list is an ordered collection of items that can be of different types. Lists are very useful and flexible data structures, and they are an essential part of any Python programmer’s toolkit.

Here is an overview of Python lists, including examples to help you get started:

  1. Creating a list: To create a list in Python, you can enclose a comma-separated list of items in square brackets. For example:
numbers = [1, 2, 3, 4, 5]
words = ['apple', 'banana', 'cherry']
mixed = [1, 'apple', 3.14, True]

2. Accessing list items: To access a specific item in a list, you can use its index. In Python, list indices start at 0, so the first item in the list has an index of 0, the second item has an index of 1, and so on. For example:

numbers = [1, 2, 3, 4, 5]
print(numbers[0]) # prints 1
print(numbers[2]) # prints 3

3. Modifying list items: To modify a specific item in a list, you can use its index to assign a new value. For example:

numbers = [1, 2, 3, 4, 5]
numbers[2] = 7
print(numbers) # prints [1, 2, 7, 4, 5]

4. Adding items to a list: To add an item to a list, you can use the append() method. This will add the item to the end of the list. For example:

numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers) # prints [1, 2, 3, 4, 5, 6]

5. Removing items from a list: To remove an item from a list, you can use the remove() method, which removes the first occurrence of the item. You can also use the pop() method, which removes and returns the item at a specific index. For example:

numbers = [1, 2, 3, 4, 5]
numbers.remove(3)
print(numbers) # prints [1, 2, 4, 5]

item = numbers.pop(1)
print(item) # prints 2
print(numbers) # prints [1, 4, 5]

6. Sorting a list: To sort a list in Python, you can use the sort() method. This will sort the list in ascending order by default. If you want to sort the list in descending order, you can pass the reverse=True argument to the sort() method. For example:

numbers = [5, 3, 2, 1, 4]
numbers.sort()
print(numbers) # prints [1, 2, 3, 4, 5]

words = ['cherry', 'banana', 'apple']
words.sort(reverse=True)
print(words) # prints ['cherry', 'banana', 'apple']

7. Reversing a list: To reverse the order of a list in Python, you can use the reverse() method. This will modify the list in place and reverse the order of the items. For example:

numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print(numbers) # prints [5, 4, 3, 2, 1]

8. Looping through a list: To loop through a list in Python, you can use a for loop. This allows you to iterate over each item in the list and perform a specific action. For example:

numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number) # prints 1, 2, 3, 4, 5

9. List comprehensions: List comprehensions are a concise way to create a list using a single line of code. They are useful for creating lists based on specific conditions or transformations. They are also the best way to replace the for loops. For example:

# create a list of the squares of the numbers from 1 to 10
squares = [x ** 2 for x in range(1, 11)]
print(squares) # prints [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

# more logic: create a list of the even squares from 1 to 10
even_squares = [x ** 2 for x in range(1, 11) if x % 2 == 0]
print(even_squares) # prints [4, 16, 36, 64, 100]

10. Nested lists: In Python, you can create lists within lists, also known as nested lists. This can be useful for representing data that has a hierarchical structure or for creating multi-dimensional arrays. For example:

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[0][1]) # prints 2

I hope these examples and tips help you get started with Python lists. Remember that lists are a powerful and flexible data structure, and they are an essential part of Python programming. With practice and experimentation, you can learn to use lists effectively to solve a wide variety of problems.

Yashuv Baskota

Python | Data Science and Machine Learning Practitioner | Learner | "The only way to do great work is to love what you do." - Steve Jobs