Learn about Python Lists

Introduction

In Python, a list is a fundamental data structure that allows you to store an ordered collection of items. These items can be of various data types, including numbers, strings, or even other lists. In this article, we will discuss Python lists in detail.

What are Python Lists?

A list is a collection of items that are ordered and mutable. It means that the items in a list are stored in a particular sequence, and we can change, add or remove elements from it. The items in a list can be of any data type, such as numbers, strings, or even other lists.

The items in a list are separated by commas and enclosed in square brackets. For example:

my_list = [1, 2, 3, 4, 5]

How to create a Python list?

To create a list in Python, we simply need to enclose a sequence of items in square brackets. Here are a few examples:

my_list1 = [1, 2, 3, 4, 5] # list of integers
my_list2 = ["apple", "banana", "cherry"] # list of strings
my_list3 = [1.23, "hello", True] # list of mixed types

As you can see, a list can contain elements of different types.

Accessing elements in a Python list

To access an element in a list, we can use its index. The index of an element in a list starts at 0. For example:

my_list = [1, 2, 3, 4, 5]
print(my_list[0]) # output: 1
print(my_list[3]) # output: 4

In the above example, we are accessing the first element of the list, which is 1, and the fourth element of the list, which is 4.

Accessing elements from the end of a list is also possible by using negative indexing. The last element has an index of -1, the second-last element has an index of -2, and so on. For example:

my_list = [1, 2, 3, 4, 5]
print(my_list[-1]) # output: 5
print(my_list[-3]) # output: 3

Slicing a Python list

We can also slice a list to access a subset of its elements. The syntax for slicing a list is list_name[start:stop:step]. For example:

my_list = [1, 2, 3, 4, 5]
print(my_list[1:4]) # output: [2, 3, 4]
print(my_list[::2]) # output: [1, 3, 5]

In the above example, we are slicing the list to get the elements from index 1 to index 3 and all the elements with an even index.

Modifying a Python List

As mentioned earlier, Python lists are mutable, which means we can add, remove, or modify elements in them. To add an element to a list, we can use the ‘append()‘ method. Here is an example:

my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # output: [1, 2, 3, 4]

In the above example, we are adding the number 4 to the end of the list using the ‘append()‘ method.

To remove an element from a list, we can use the ‘remove()‘ method. Here is an example:

my_list = [1, 2, 3, 4]
my_list.remove(2)
print(my_list) # output: [1, 3, 4]

List Methods

Python also provides several built-in list methods that can be used to manipulate and modify the contents of a list. Here, we will explore some of the commonly used Python list methods.

1. ‘append()‘: This method is used to add an item to the end of the list. The syntax is as follows:

list_name.append(item)

2. ‘extend()‘: This method is used to add multiple items to the end of a list. The syntax is as follows:

list_name.extend(iterable)

Here, ‘list_name‘ is the name of the list, and ‘iterable‘ is an iterable object like a list, tuple, or string.

3. ‘insert()‘: This method is used to insert an item at a specific index in the list. The syntax is as follows:

list_name.insert(index, item)

Here, ‘list_name‘ is the name of the list, ‘index‘ is the index at which the item needs to be inserted, and ‘item‘ is the value that needs to be inserted.

4. ‘remove()‘: This method is used to remove the first occurrence of a specific value from the list. The syntax is as follows:

list_name.remove(item)

Here, ‘list_name‘ is the name of the list, and ‘item‘ is the value that needs to be removed.

5. ‘pop()‘: This method is used to remove and return an item from a specific index in the list. The syntax is as follows:

list_name.pop(index)

Here, ‘list_name‘ is the name of the list, and ‘index‘ is the index of the item that needs to be removed.

6. ‘del‘: This method is used to delete the entire list.

del list_name

It also can be used to remove an item from a specific location.

del list_name[index]

If you mention the location inside the square brackets, it will delete the item from the list from that position.

7. ‘clear()‘: This method is used to remove all the items from the list. The syntax is as follows:

list_name.clear()

8. ‘sort()‘: This method is used to sort the items in the list in ascending order. The syntax is as follows:

list_name.sort()

9. ‘reverse()‘: This method is used to reverse the order of the items in the list. The syntax is as follows:

list_name.reverse()

10. ‘len()‘: It returns the total number of elements present in a list or the length of the list.

len(list_name)

11. ‘count()‘: It returns the number of times an item appears in the list.

list_name.count(item)

These are some of the commonly used Python list methods.

Exercises

Try some exercises for practicing Python lists.

  1. Write a program that creates a list of integers from 1 to 10 and prints it out.
  2. Write a program that creates an empty list and allows the user to add elements to it one by one until they enter “stop”. Then, print out the list.
  3. Write a program that creates a list of integers from 1 to 10 and then prints out every other element in the list.
  4. Write a program that creates a list of integers and then removes all even numbers from the list.
  5. Write a program that creates a list of integers and then sorts it in descending order.
  6. Write a program that creates two lists of integers and then combines them into one list.
  7. Write a program that creates a list of strings and then prints out the length of each string in the list.
  8. Write a program that creates a list of integers and then prints out the sum of all the numbers in the list.
  9. Write a program that creates a list of integers and then finds the largest number in the list.
  10. Write a program that creates a list of integers and then finds the average of all the numbers in the list.

These exercises will help you practice working with Python lists. Good luck!

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 147