Lists in Python

·

3 min read

Lists in Python

Python lists are an essential data type in Python that can store a collection of items. Lists are mutable, meaning you can modify them in various ways.

Creating a List in Python

In Python, a list is defined using square brackets []. To create a new list, we simply place the items we want to include in the list within the brackets, separated by commas. For example, the following code creates a list of integers:

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

We can also create a list of strings, or even a list that contains a mix of different data types:

my_list2 = ['apple', 'banana', 'cherry']
my_list3 = [1, 'two', 3.0, True]

Accessing Elements in a List

Once we have created a list, we can access individual elements within it using their index positions. In Python, list indices start at 0, so the first item in a list has an index of 0, the second item has an index of 1, and so on. We can use square brackets to access a specific item in the list, like this:

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

We can also use negative indices to access items from the end of the list. For example:

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

Modifying Lists

One of the most powerful features of Python lists is their ability to be modified. We can add new items to a list using the append() method, which adds an item to the end of the list:

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

We can also insert an item into a specific position in the list using the insert() method:

my_list = [1, 2, 3, 4, 5]
my_list.insert(2, 'new')
print(my_list)   # Output: [1, 2, 'new', 3, 4, 5]

To remove an item from a list, we can use the remove() method, which removes the first occurrence of the specified item:

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

We can also use the pop() method to remove an item from a specific index position. If we don't specify an index, pop() will remove the last item in the list:

my_list = [1, 2, 3, 4, 5]
my_list.pop(2)
print(my_list)   # Output: [1, 2, 4, 5]

my_list.pop()
print(my_list)   # Output: [1, 2, 4]

List Unpacking


my_list = [1, 2, 3, 4, 5]
first, *rest = my_list
print(first)
print(rest)

Output:

1
[2, 3, 4, 5]

In this example, the variable first is assigned the value of the first element in the my_list list, which is 1. The remaining elements in the list are assigned to the rest variable using the asterisk (*) operator.

Note that the asterisk operator can be used in any position in the list, as long as there is only one asterisk used. For example:

*start, last = my_list
print(start)
print(last)

Output:

[1, 2, 3, 4]
5

In this example, the variable last is assigned the value of the last element in the my_list list, which is 5. The remaining elements in the list are assigned to the start variable using the asterisk (*) operator.