One of the most fundamental data structures in any programming language is the list. In Python, lists are simply a collection of items organized in a sequence.
They’re essential and versatile tools for working with various data types, making them a critical part of any programmer’s toolkit. In this tutorial, you’ll learn how to store a list in Python and explore different ways to use, access, and manipulate elements in a list.
Creating a List
Creating a list in Python is as simple as defining a variable, enclosing it with square brackets, and separating the elements using commas. Here’s an example:
1 |
fruits = ['apple', 'banana', 'cherry', 'orange'] |
The list fruits
now stores four strings. Lists can also store different types of elements, such as integers, floats, or even other lists:
1 |
mixed_list = [42, 'hello', 3.14, [1, 2, 3]] |
You can even create an empty list and later add elements to it:
1 |
empty_list = [] |
Accessing List Elements
To retrieve an element from a list, you use its index – a numerical value that represents an item’s position in the list. Indexing starts at 0, as with most programming languages, so the first item has an index of 0, the second has an index of 1, and so on. Here’s an example:
1 2 3 |
fruits = ['apple', 'banana', 'cherry', 'orange'] print(fruits[0]) # Output: apple print(fruits[2]) # Output: cherry |
Python also supports negative indexing, which starts at the end of the list and works backward:
1 2 3 |
fruits = ['apple', 'banana', 'cherry', 'orange'] print(fruits[-1]) # Output: orange print(fruits[-3]) # Output: banana |
Slicing Lists
If you need to access a range of elements, you can use slicing. By providing two indices separated by a colon, you can extract a subsequence of a list:
1 2 |
fruits = ['apple', 'banana', 'cherry', 'orange'] print(fruits[1:3]) # Output: ['banana', 'cherry'] |
In the example above, the slice extracts elements from index 1 (inclusive) to index 3 (exclusive). You can omit either index to slice from the beginning or to the end of the list:
1 2 3 |
fruits = ['apple', 'banana', 'cherry', 'orange'] print(fruits[:2]) # Output: ['apple', 'banana'] print(fruits[2:]) # Output: ['cherry', 'orange'] |
Modifying List Elements
Lists in Python are mutable, meaning you can change their contents after creation. To modify a list element, simply assign a new value to its index:
1 2 3 |
fruits = ['apple', 'banana', 'cherry', 'orange'] fruits[1] = 'blueberry' print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'orange'] |
Adding and Removing Elements
To add a new element to a list, you can use the append()
method:
1 2 3 |
fruits = ['apple', 'banana', 'cherry', 'orange'] fruits.append('grape') print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange', 'grape'] |
To remove an element from a list, you can use the remove()
method – but keep in mind that it removes the first occurrence of the supplied value:
1 2 3 |
fruits = ['apple', 'banana', 'cherry', 'orange', 'banana'] fruits.remove('banana') print(fruits) # Output: ['apple', 'cherry', 'orange', 'banana'] |
If you need to remove an element by its index, use the pop()
method:
1 2 3 4 |
fruits = ['apple', 'banana', 'cherry', 'orange'] removed_element = fruits.pop(1) print(removed_element) # Output: banana print(fruits) # Output: ['apple', 'cherry', 'orange'] |
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
fruits = ['apple', 'banana', 'cherry', 'orange'] mixed_list = [42, 'hello', 3.14, [1, 2, 3]] empty_list = [] print(fruits[0]) print(fruits[2]) print(fruits[-1]) print(fruits[-3]) print(fruits[1:3]) print(fruits[:2]) print(fruits[2:]) fruits[1] = 'blueberry' print(fruits) fruits.append('grape') print(fruits) fruits.remove('blueberry') print(fruits) removed_element = fruits.pop(1) print(removed_element) print(fruits) |
Conclusion
In this tutorial, you learned how to store and manipulate lists in Python. Lists are an essential data structure for any programmer, as they allow you to store and work with various data types easily. You’ve seen how to create, access, and modify lists, as well as how to add and remove elements. Remember that practice makes perfect, so try implementing different list operations in your Python programs!