Everything You Need to Know About Python Lists

Python stands out as a versatile and beginner-friendly language, gaining immense popularity due to its simplicity, readability, and extensive range of applications. Lists are one of its built-in data structures. In this blog, we will learn about Python lists and its methods.

Let’s start!

What is a List?

In Python, a list is an ordered collection of data items. In a list, you can store multiple items of different data types, including numbers, strings and even other lists. List items are separated by commas and enclosed within square brackets [].

Example: 1

A list containing numbers.

Example: 2

A list containing strings.

Example: 3

A list containing different data types.

Example: 4

A list containing another list.

Lists are mutable which means you can change them after creation.

Creating Lists

You can create lists in the following ways.

  • List literals
  • Using the list() constructor
  • List comprehension

List literals

You can create lists by defining the list items within square brackets [], separated by commas.

For example:

Using the list() constructor

You can create a list using the list() constructor, which converts other iterable objects like strings, other lists, tuples, dictionaries or sets into lists.

For example:

List comprehension

You can create lists based on existing iterables using list comprehension.

Syntax:

Here,

Expression: It is the item that is being iterated.

Iterable: It can be any iterable object like lists, tuples, dictionaries, sets, or strings.

Condition: Condition checks if the item should be added to the new list or not.

For example:

Finding the Length of the List

You can find the length of a list (number of items present in a list) using the len() function.

For example:

Accessing List Items

In lists, each item has its unique index. You can access list items using indexing. Indexing starts from 0 which means the first element has an index of 0, the second element has an index of 1, and so on.

For example:

You can also access items using negative indexing which means from the end of the list. -1 for the last item, -2 for the second-to-last item, and so on.

For example:

To understand negative indexing you can consider fruits[-1] as fruits[len(fruits)-1].

Example:

Check Whether an Item is Present in the List

You can check whether an item is present in the list or not, using the in keyword.

Example: 1

Example: 2

List Slicing

You can slice a list by giving start, end and step(skip) parameters.

Syntax:

Here,

start: This is the index where you want to begin slicing. If you don’t specify it, it starts from the beginning (index 0).

end: This is the index where you want to stop slicing. The slice will include elements up to, but not including, this index. If you don’t specify it, it goes to the end of the list.

step: The step size is how many elements you skip between slices. If you don’t mention it, it’s assumed to be 1, which means you include consecutive elements.

Example: 1

Here, the start index is 2 and the end index is 5 which means it will print the numbers starting from index 2 i.e. 13 and ending at index (5-1) i.e. 18.

You can also use negative indexing.

Here, the start index is -2 and the end index is 5 which means it will print the numbers starting from index -2 i.e. 18 and ending at index (5-1) i.e. 18.

You can calculate the negative index as numbers[-2] as numbers[len(numbers)-2] as explained in accessing list items.

Example: 2

Here, the start index is 2 and the end index is not given. So it will print the items starting from index 2 and till the end.

You can also use negative indexing.

Example: 3

Here, the start index is not given and the end index is 4. So it will print the items from start to index 3 i.e. (4-1).

You can also use negative indexing.

Example: 4

Here, the start index is 1, the end index is 7 and the step index is 2. So it will print the items starting from index 1, ending at index 6 i.e. (7-1) and with a step of 2.

List Methods

Python provides the following built-in methods to manipulate list items.

sort()

This method is used to sort the list in ascending order or alphabetically.

Example:

What if you want to sort the list in descending order?

To sort the list in descending order, you have to give reverse=True as a parameter in the sort method.

For example:

Note: reverse is a parameter. Python also provides a reverse method to reverse the order of the list. Do not mistake the reverse parameter with the reverse method, as the reverse parameter is for sorting a list in descending order while the reverse method is for reversing the order of the list.

index()

This method returns the index number of the first occurrence of the given list item.

Example:

append()

This method is used to append items to the end of the existing list.

Example:

extend()

This method is used to add an entire list or any other collection datatype (set, tuple, dictionary) to the existing list.

Example:

insert()

This method is used to insert an item to the list at the given index.

Example:

copy()

This method returns a copy of the list. This will not change the original list.

Example:

count()

This method returns how many times a certain item has appeared.

Example:

reverse()

This method is used to reverse the order of the list.

Example:

clear()

This method is used to remove all items from a list.

Example:

pop()

This method is used to remove the last item of the list and return the removed item.

Example:

Concatenating Lists

You can concatenate two or more lists using the + operator.

Example:

Conclusion

In this blog, you’ve learned about Python lists, one of the fundamental data structures in the Python programming language.

Lists allow you to store and manipulate collections of data, whether they’re numbers, strings, or even other lists. This post covers the basics of lists, creating lists, accessing their items, list slicing and list methods.

Thanks for reading.

For more content like this click here.

You can also follow me on X(Twitter) for getting daily tips on web development.

Keep Coding!!

Leave a Comment