Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to define a list in Python

Introduction

Welcome to this blog post on defining lists in Python. If you are new to programming or just starting with Python, this article is for you! We will walk you through the process of creating and manipulating lists in Python, giving you a solid foundation to build upon. We will avoid using jargon and technical terms that might be confusing, and if we do use them, we will make sure to explain them clearly. Our aim is to make it as easy as possible for you to understand the concepts and start working with lists in Python.

What is a List?

In programming, a list is a collection of items that can be ordered and changed. In Python, a list is a data structure that can store multiple items of different data types like integers, strings, or even other lists.

Imagine you have a shopping list written down on a piece of paper. You can add items to the list, cross them off when you've bought them, and even reorder the items based on their priority. A list in Python works similarly, allowing you to store and manage items in an organized way.

Creating a List

To create a list in Python, you can simply put comma-separated values inside square brackets []. Here's an example of an empty list and a list with some items:

# An empty list
empty_list = []

# A list with some items
fruits = ["apple", "banana", "cherry"]

You can also create a list using the list() constructor. The list() constructor is a built-in function in Python that creates a new list object. Here's how you can use it:

# Using the list() constructor
empty_list = list()

# Creating a list with some items
numbers = list((1, 2, 3, 4, 5))

In the second example, we pass a tuple (another data structure in Python) containing the numbers 1 to 5 to the list() constructor. The constructor then creates a list with the same items.

Accessing List Items

To access items in a list, you can use their index number. The index number is the position of the item in the list, starting from 0. So, in a list with three items, the first item has an index of 0, the second item has an index of 1, and the third item has an index of 2.

fruits = ["apple", "banana", "cherry"]

# Accessing the first item
first_item = fruits[0]
print(first_item)  # Output: apple

# Accessing the second item
second_item = fruits[1]
print(second_item)  # Output: banana

# Accessing the third item
third_item = fruits[2]
print(third_item)  # Output: cherry

You can also access items from the end of the list using negative index numbers. This is helpful when you don't know the length of the list or when you want to access items from the end without calculating the exact index.

fruits = ["apple", "banana", "cherry"]

# Accessing the last item
last_item = fruits[-1]
print(last_item)  # Output: cherry

# Accessing the second last item
second_last_item = fruits[-2]
print(second_last_item)  # Output: banana

Modifying List Items

You can change the value of a specific item in a list by referring to its index number. Here's an example:

fruits = ["apple", "banana", "cherry"]

# Changing the second item
fruits[1] = "blueberry"
print(fruits)  # Output: ['apple', 'blueberry', 'cherry']

In this example, we changed the second item (with an index of 1) from "banana" to "blueberry".

Adding Items to a List

To add items to a list, you can use the append() method. The append() method adds an item to the end of the list.

fruits = ["apple", "banana", "cherry"]

# Adding an item to the list
fruits.append("orange")
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'orange']

You can also insert an item at a specific position in the list using the insert() method. The insert() method takes two arguments: the index at which you want to insert the item and the item itself.

fruits = ["apple", "banana", "cherry"]

# Inserting an item at position 1
fruits.insert(1, "orange")
print(fruits)  # Output: ['apple', 'orange', 'banana', 'cherry']

In this example, we inserted the item "orange" at position 1 (between "apple" and "banana").

Removing Items from a List

To remove an item from a list, you can use the remove() method. The remove() method takes the item you want to remove as an argument and removes the first occurrence of that item from the list.

fruits = ["apple", "banana", "cherry", "banana"]

# Removing an item from the list
fruits.remove("banana")
print(fruits)  # Output: ['apple', 'cherry', 'banana']

If you want to remove an item at a specific position, you can use the pop() method. The pop() method takes the index of the item you want to remove as an argument and removes that item from the list.

fruits = ["apple", "banana", "cherry"]

# Removing the second item
fruits.pop(1)
print(fruits)  # Output: ['apple', 'cherry']

If no argument is passed to the pop() method, it will remove the last item from the list.

Sorting a List

You can sort a list using the sort() method. The sort() method sorts the items in the list in ascending order (from lowest to highest).

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

# Sorting the list
numbers.sort()
print(numbers)  # Output: [1, 2, 3, 4, 5]

You can also sort a list in descending order (from highest to lowest) by passing the reverse=True argument to the sort() method.

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

# Sorting the list in descending order
numbers.sort(reverse=True)
print(numbers)  # Output: [5, 4, 3, 2, 1]

Keep in mind that the sort() method modifies the original list. If you want to create a new sorted list without changing the original one, you can use the sorted() function.

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

# Creating a new sorted list
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # Output: [1, 2, 3, 4, 5]

Conclusion

In this blog post, we covered the basics of defining and working with lists in Python. We hope that this article has helped you understand how to create, access, modify, and manipulate lists in Python. With this knowledge, you are now ready to tackle more complex programming tasks and build upon your skills.

Remember, practice makes perfect. Try working with lists on your own and experiment with the different methods and functions we discussed. Happy coding!