Python List

1. What is List in Python

A Python list is a built-in data structure that can store multiple items in a single variable. Lists are created by placing a comma-separated sequence of sub-items enclosed in square brackets [ ].

The primary characteristics of Python lists are their mutability, which means they can be changed after they are created, and their ability to hold different data types. The elements of a list can be of any type, including numbers, strings, and even other lists.

Here a basic example of Python List

my_list = [1, 2, 'apple', 4.5]


[1, 2, 'apple', 4.5]

2. How to Create a List in Python

Python lists are straightforward to create. At its simplest, a list can be defined by enclosing a comma-separated sequence of items in square brackets [ ].

Example 1 - Creating a list using different datatypes

# Creating an integer list
integer_list = [1, 2, 3, 4, 5]

# Creating a float list
float_list = [1.2, 2.3, 3.4, 4.5, 5.6]

# Creating a string list
string_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']


# integer_list
[1, 2, 3, 4, 5]

# float_list 
[1.2, 2.3, 3.4, 4.5, 5.6]

# string_list 
['apple', 'banana', 'cherry', 'date', 'elderberry']

Example 2 - Creating a list using mixed and duplicate items

# List with mixed types
mixed_list = [1, 'two', 3.0, True, [5, 6, 7]]

# List with duplicate items 
dup_list = [1, 2, 2, 3, 4, 4, 4, 5, 5, 5, 5]

# List with duplicate items (strings)
dup_list_strings = ['apple', 'banana', 'apple', 'cherry', 'banana', 'cherry']


# List with mixed types
[1, 'two', 3.0, True, [5, 6, 7]]

# List with duplicate items 
[1, 2, 2, 3, 4, 4, 4, 5, 5, 5, 5]

# List with duplicate items 
['apple', 'banana', 'apple', 'cherry', 'banana', 'cherry']

3. Manipulating Python Lists

Python lists offer robust options for manipulation. Elements in a list are assigned a unique index based on their position, starting from 0 for the first element. Using these indices, we can access, add, modify, or remove elements from the list. Slicing, another powerful feature, allows for accessing subsets of the list using a range of indices.

3.1. Accessing the items in List

You can get to access any element in the list by referring to its index number. Remember, indexing in Python starts from 0, which means the first element is at position 0, the second at position 1, and so on.

Here's an example:

my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']



Example of multi-dimensional list:

two_dim_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

two_dim_list_str = [["apple", "banana", "cherry"], ["dog", "cat", "mouse"]]



3.2. Negative Indexing

Python also supports negative indexing, which allows you to start the index from the end of the list. -1 refers to the last item, -2 refers to the second-last item, and so on. Here's how you can use negative indexing:

my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']



4. Slicing of a List

In Python, Slicing allows you to retrieve a section, or slice, of a list. You can slice a list by specifying the start index and the end index separated by a colon ‘:’. The start index is inclusive, and the end index is exclusive.


# Define a list of numbers
num_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

# Slicing from index 2 to 5 (exclusive)
print(num_list[2:5])   # Output: [30, 40, 50]

# Slicing from the start to index 3 (exclusive)
print(num_list[:3])    # Output: [10, 20, 30]

# Slicing from index 6 to the end
print(num_list[6:])    # Output: [70, 80, 90, 100]

# Slicing the last three items
print(num_list[-3:])   # Output: [80, 90, 100]

# Slicing every second item
print(num_list[::2])   # Output: [10, 30, 50, 70, 90]


[30, 40, 50]

[10, 20, 30]

[70, 80, 90, 100]

[80, 90, 100]

[10, 30, 50, 70, 90]

5. Updating a List

Lists in Python are mutable, so we can update the list using an assignment operator (=).


my_list_1 = ['apple', 'banana', 'cherry']
my_list[1] = 'blackberry'

my_list_2 = ['apple', 'banana', 'cherry', 'date', 'elderberry']
my_list[1:3] = ['blackberry', 'citrus']


['apple', 'blackberry', 'cherry']

['apple', 'blackberry', 'citrus', 'date', 'elderberry']

6. Adding an item to a List

There are 3 ways to add elements to a list in Python. They are:

6.1. Append Method

The append() method adds an element to the end of the list.

my_list = ['apple', 'banana', 'cherry']


['apple', 'banana', 'cherry', 'date']

In this example, 'date' is added to the end of the list.

6.2. Insert Method

The insert() method adds an element at a specified index. This method requires two arguments: the index at which to insert the new element and the new element itself.

my_list = ['apple', 'cherry', 'date']
# inserting an element at index 1 (second position)
my_list.insert(1, 'banana')


['apple', 'banana', 'cherry', 'date']

6.3. Extend Method

The extend() method adds multiple elements to the end of the list. This method takes an iterable (like a list or a tuple) as an argument.

my_list = ['apple', 'banana', 'cherry']
my_list.extend(['date', 'elderberry'])


['apple', 'banana', 'cherry', 'date', 'elderberry']

7. Removing an item from the List

7.1. Remove Method

If you know the value of the item, you can use the remove() method, which removes the first occurrence of a value.

my_list = ['apple', 'banana', 'cherry']


['apple', 'cherry']

7.2. Pop Method

If you know the index of the item, you can use the pop() method, which removes the item at a specific index and returns it.

my_list = ['apple', 'banana', 'cherry']


['apple', 'cherry']

Here, the pop() method removes the item at index 1, which is 'banana'.

If no index is specified, pop() removes and returns the last item in the list.

7.3. Del Keyword

The del keyword in Python can also be used to remove items from a list. Like pop(), del removes an item at a specific index:

my_list = ['apple', 'banana', 'cherry']
del my_list[1]


['apple', 'cherry']

The del keyword can also remove slices from a list or delete the entire list.

# remove a slice
my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry']
del my_list[1:3]  print(my_list)

# delete entire list
del my_list


['apple', 'date', 'elderberry']

8. Iterating through a List

Iterating through a list in Python is a common operation that allows you to access each item in the list, one at a time.

The for loop in Python iterates over the items of a sequence in the order that they appear.

my_list = ['apple', 'banana', 'cherry']
for item in my_list:



9. List comprehension

List comprehension in Python is a powerful and compact way to create new lists by transforming the elements of an existing list (or any iterable) in a single, readable line of code.

Here’s an example of a list comprehension

# Example 1: Squares of all numbers in the list
numbers = [1, 2, 3, 4, 5]
squares = [n**2 for n in numbers]

# Example 2: Squares of numbers greater than 2 in the list
numbers = [1, 2, 3, 4, 5]
squares = [n**2 for n in numbers if n > 2]


[1, 4, 9, 16, 25]

9, 16, 25]

10. Let’s Revise

Python Lists:

  • A Python list is a data structure for storing multiple items in a single variable.
  • Lists are created using square brackets [ ] and can contain various data types.
  • Key characteristics: mutability (can be changed after creation) and the ability to hold different data types.

Creating Lists:

  • Lists are created by enclosing items in square brackets.
  • Examples: integer list, float list, string list, mixed-type list, and lists with duplicate items.

Accessing List Items:

  • Items in a list are accessed using their index, starting from 0.
  • Negative indexing starts from the end of the list.
  • Multi-dimensional lists use nested indexing.

Slicing Lists:

  • Slicing retrieves a section of a list by specifying start and end indices.
  • The start index is inclusive, and the end index is exclusive.
  • Slicing can create sublists.

Updating Lists:

  • Lists are mutable, allowing elements to be changed using the assignment operator.
  • Elements can be replaced individually or within a range.

Adding Items to Lists:

  • Three ways to add elements: append(), insert(), and extend().
  • append() adds an element to the end.
  • insert() adds an element at a specified index.
  • extend() adds multiple elements from an iterable to the end.

Removing Items from Lists:

  • Three methods: remove(), pop(), and del.
  • remove() removes the first occurrence of a value.
  • pop() removes and returns an item at a specified index.
  • del removes items by index or deletes the entire list.

Iterating Through Lists:

  • Loop through a list using a for loop.
  • Access each item in the list one at a time.

List Comprehension:

  • A concise way to create new lists by transforming elements from an existing list.
  • Allows filtering elements during creation.

11. Test Your Knowledge

1. What is the primary characteristic of a Python list?
2. How is a Python list created?
3. What is the starting index for elements in a Python list?
4. Which of the following list operations creates a sublist?
5. In Python, what does negative indexing refer to?
6. What is the purpose of list comprehension in Python?
7. Which list method removes the first occurrence of a specific value from a list?
8. Which of the following methods adds multiple elements to the end of a Python list?