List in Python
Mastering Lists in Python: A Comprehensive Exploration
Lists in Python are versatile and powerful data structures that play a crucial role in many programming scenarios. Whether you’re a Python beginner or an experienced developer, understanding lists and their various operations is fundamental. In this comprehensive guide, we will explore the ins and outs of Python lists, covering creation, manipulation, iteration, and best practices.
Introduction to Lists
A list in Python is an ordered collection of elements. These elements can be of any data type, including numbers, strings, or even other lists. Lists are mutable, meaning their elements can be modified after creation. They provide a flexible and dynamic way to organize and manipulate data.
Creating Lists
Lists are created using square brackets [][] and commas ,, to separate elements. For example, the following code creates a list of integers:
list_integers = [1, 2, 3, 4, 5]
print(list_integers)list_integers = [1, 2, 3, 4, 5]
print(list_integers)Output:
C:\Users\username>python list_integers.py
[1, 2, 3, 4, 5]C:\Users\username>python list_integers.py
[1, 2, 3, 4, 5]Properties of Lists
Lists in Python have the following properties:
- Lists are ordered: The elements in a list are stored in a particular order. This order is preserved until the list is modified.
- Lists are mutable: The elements in a list can be modified after creation.
- Lists are heterogeneous: The elements in a list can be of any data type.
- Lists can contain duplicate elements: The same element can appear multiple times in a list.
- Lists can be nested: A list can contain other lists as elements.
- Lists are dynamic: The size of a list is not fixed. It can grow or shrink as needed.
- Lists are iterable: The elements in a list can be accessed using a for loop.
- Lists are zero-indexed: The first element in a list has an index of 0.
Declaring Lists
Lists can be declared in several ways. The simplest way is to use square brackets [][] and commas ,, to separate elements.
Declaring an Empty List
An empty list can be declared using empty square brackets [][]:
empty_list = []
print(empty_list)empty_list = []
print(empty_list)Output:
C:\Users\username>python empty_list.py
[]C:\Users\username>python empty_list.py
[]In this example, we declare an empty list and assign it to the variable empty_listempty_list. We then print the list to the console. The output shows that the list is empty.
Declaring a List with Elements
A list with elements can be declared using square brackets [][] and commas ,, to separate elements:
list_with_elements = [1, 2, 3, 4, 5]
print(list_with_elements)list_with_elements = [1, 2, 3, 4, 5]
print(list_with_elements)Output:
C:\Users\username>python list_with_elements.py
[1, 2, 3, 4, 5]C:\Users\username>python list_with_elements.py
[1, 2, 3, 4, 5]In this example, we declare a list with five elements and assign it to the variable list_with_elementslist_with_elements. We then print the list to the console. The output shows that the list contains five elements.
Declaring a List with Duplicate Elements
A list with duplicate elements can be declared using square brackets [][] and commas ,, to separate elements:
list_with_duplicate_elements = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(list_with_duplicate_elements)list_with_duplicate_elements = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(list_with_duplicate_elements)Output:
C:\Users\username>python list_with_duplicate_elements.py
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]C:\Users\username>python list_with_duplicate_elements.py
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]In this example, we declare a list with ten elements, including duplicates, and assign it to the variable list_with_duplicate_elementslist_with_duplicate_elements. We then print the list to the console. The output shows that the list contains ten elements, including duplicates.
Declaring a List with Elements of Different Data Types
A list with elements of different data types can be declared using square brackets [][] and commas ,, to separate elements:
list_with_elements_of_different_data_types = [1, 2.0, "three", [4, 5]]
print(list_with_elements_of_different_data_types)list_with_elements_of_different_data_types = [1, 2.0, "three", [4, 5]]
print(list_with_elements_of_different_data_types)Output:
C:\Users\username>python list_with_elements_of_different_data_types.py
[1, 2.0, 'three', [4, 5]]C:\Users\username>python list_with_elements_of_different_data_types.py
[1, 2.0, 'three', [4, 5]]In this example, we declare a list with four elements of different data types and assign it to the variable list_with_elements_of_different_data_typeslist_with_elements_of_different_data_types. We then print the list to the console. The output shows that the list contains four elements of different data types.
Declaring a List with Elements of the Same Data Type
A list with elements of the same data type can be declared using square brackets [][] and commas ,, to separate elements:
list_with_elements_of_same_data_type = [1, 2, 3, 4, 5]
print(list_with_elements_of_same_data_type)list_with_elements_of_same_data_type = [1, 2, 3, 4, 5]
print(list_with_elements_of_same_data_type)Output:
C:\Users\username>python list_with_elements_of_same_data_type.py
[1, 2, 3, 4, 5]C:\Users\username>python list_with_elements_of_same_data_type.py
[1, 2, 3, 4, 5]In this example, we declare a list with five elements of the same data type and assign it to the variable list_with_elements_of_same_data_typelist_with_elements_of_same_data_type. We then print the list to the console. The output shows that the list contains five elements of the same data type.
Declaring a List with a Single Element
A list with a single element can be declared using square brackets [][] and commas ,, to separate elements:
list_with_single_element = [1]
print(list_with_single_element)list_with_single_element = [1]
print(list_with_single_element)Output:
C:\Users\username>python list_with_single_element.py
[1]C:\Users\username>python list_with_single_element.py
[1]In this example, we declare a list with a single element and assign it to the variable list_with_single_elementlist_with_single_element. We then print the list to the console. The output shows that the list contains a single element.
Declaring a List with Multiple Lines
A list with multiple lines can be declared using square brackets [][] and commas ,, to separate elements:
list_with_multiple_lines = [
1,
2,
3,
4,
5
]
print(list_with_multiple_lines)list_with_multiple_lines = [
1,
2,
3,
4,
5
]
print(list_with_multiple_lines)Output:
C:\Users\username>python list_with_multiple_lines.py
[1, 2, 3, 4, 5]C:\Users\username>python list_with_multiple_lines.py
[1, 2, 3, 4, 5]In this example, we declare a list with five elements on multiple lines and assign it to the variable list_with_multiple_lineslist_with_multiple_lines. We then print the list to the console. The output shows that the list contains five elements.
Declaring a List with list()
A list can be declared using the built-in list()list() function:
list_with_list_function = list([1, 2, 3, 4, 5])
print(list_with_list_function)list_with_list_function = list([1, 2, 3, 4, 5])
print(list_with_list_function)Output:
C:\Users\username>python list_with_list_function.py
[1, 2, 3, 4, 5]C:\Users\username>python list_with_list_function.py
[1, 2, 3, 4, 5]In this example, we declare a list using the built-in list()list() function and assign it to the variable list_with_list_functionlist_with_list_function. We then print the list to the console. The output shows that the list contains five elements.
Declaring a List with no elements using list()
A list with no elements can be declared using the built-in list()list() function:
empty_list_with_list_function = list()
print(empty_list_with_list_function)empty_list_with_list_function = list()
print(empty_list_with_list_function)Output:
C:\Users\username>python empty_list_with_list_function.py
[]C:\Users\username>python empty_list_with_list_function.py
[]In this example, we declare an empty list using the built-in list()list() function and assign it to the variable empty_list_with_list_functionempty_list_with_list_function. We then print the list to the console. The output shows that the list is empty.
Declaring a List with a Range of Numbers
A list with a range of numbers can be declared using the built-in range()range() function:
list_with_range_function = list(range(1, 6))
print(list_with_range_function)list_with_range_function = list(range(1, 6))
print(list_with_range_function)Output:
C:\Users\username>python list_with_range_function.py
[1, 2, 3, 4, 5]C:\Users\username>python list_with_range_function.py
[1, 2, 3, 4, 5]In this example, we declare a list with a range of numbers using the built-in range()range() function and assign it to the variable list_with_range_functionlist_with_range_function. We then print the list to the console. The output shows that the list contains five elements.
Declaring a List with a Range of Numbers and a Step
A list with a range of numbers and a step can be declared using the built-in range()range() function:
list_with_range_function_and_step = list(range(1, 6, 2))
print(list_with_range_function_and_step)list_with_range_function_and_step = list(range(1, 6, 2))
print(list_with_range_function_and_step)Output:
C:\Users\username>python list_with_range_function_and_step.py
[1, 3, 5]C:\Users\username>python list_with_range_function_and_step.py
[1, 3, 5]In this example, we declare a list with a range of numbers and a step using the built-in range()range() function and assign it to the variable list_with_range_function_and_steplist_with_range_function_and_step. We then print the list to the console. The output shows that the list contains three elements.
Declaring a List with a Range of Numbers in Reverse Order
A list with a range of numbers in reverse order can be declared using the built-in range()range() function:
list_with_range_function_and_reverse_order = list(range(5, 0, -1))
print(list_with_range_function_and_reverse_order)list_with_range_function_and_reverse_order = list(range(5, 0, -1))
print(list_with_range_function_and_reverse_order)Output:
C:\Users\username>python list_with_range_function_and_reverse_order.py
[5, 4, 3, 2, 1]C:\Users\username>python list_with_range_function_and_reverse_order.py
[5, 4, 3, 2, 1]In this example, we declare a list with a range of numbers in reverse order using the built-in range()range() function and assign it to the variable list_with_range_function_and_reverse_orderlist_with_range_function_and_reverse_order. We then print the list to the console. The output shows that the list contains five elements.
Declare a List using loop
A list can be declared using a loop:
list_with_loop = []
for i in range(1, 6):
list_with_loop.append(i)
print(list_with_loop)list_with_loop = []
for i in range(1, 6):
list_with_loop.append(i)
print(list_with_loop)Output:
C:\Users\username>python list_with_loop.py
[1, 2, 3, 4, 5]C:\Users\username>python list_with_loop.py
[1, 2, 3, 4, 5]In this example, we declare a list using a loop and assign it to the variable list_with_looplist_with_loop. We then print the list to the console. The output shows that the list contains five elements.
Declaring a List with a List Comprehension
A list can be declared using a list comprehension:
list_with_list_comprehension = [i for i in range(1, 6)]
print(list_with_list_comprehension)list_with_list_comprehension = [i for i in range(1, 6)]
print(list_with_list_comprehension)Output:
C:\Users\username>python list_with_list_comprehension.py
[1, 2, 3, 4, 5]C:\Users\username>python list_with_list_comprehension.py
[1, 2, 3, 4, 5]In this example, we declare a list using a list comprehension and assign it to the variable list_with_list_comprehensionlist_with_list_comprehension. We then print the list to the console. The output shows that the list contains five elements.
Declaring a Multi Dimensional List
A multi dimensional list can be declared using square brackets [][] and commas ,, to separate elements:
multi_dimensional_list = [[1, 2, 3], [4, 5, 6]]
print(multi_dimensional_list)multi_dimensional_list = [[1, 2, 3], [4, 5, 6]]
print(multi_dimensional_list)Output:
C:\Users\username>python multi_dimensional_list.py
[[1, 2, 3], [4, 5, 6]]C:\Users\username>python multi_dimensional_list.py
[[1, 2, 3], [4, 5, 6]]In this example, we declare a multi dimensional list and assign it to the variable multi_dimensional_listmulti_dimensional_list. We then print the list to the console. The output shows that the list contains two lists as elements. This is also known as a two-dimensional list.
Declaring a List with a List Comprehension and Nested Lists
A list with a list comprehension and nested lists can be declared using square brackets [][] and commas ,, to separate elements:
list_with_list_comprehension_and_nested_lists = [[i for i in range(1, 4)], [i for i in range(4, 7)]]
print(list_with_list_comprehension_and_nested_lists)list_with_list_comprehension_and_nested_lists = [[i for i in range(1, 4)], [i for i in range(4, 7)]]
print(list_with_list_comprehension_and_nested_lists)Output:
C:\Users\username>python list_with_list_comprehension_and_nested_lists.py
[[1, 2, 3], [4, 5, 6]]C:\Users\username>python list_with_list_comprehension_and_nested_lists.py
[[1, 2, 3], [4, 5, 6]]In this example, we declare a list with a list comprehension and nested lists and assign it to the variable list_with_list_comprehension_and_nested_listslist_with_list_comprehension_and_nested_lists. We then print the list to the console. The output shows that the list contains two lists as elements. This is also known as a two-dimensional list.
Declaring a List with a List Comprehension and Nested Lists in Reverse Order
A list with a list comprehension and nested lists in reverse order can be declared using square brackets [][] and commas ,, to separate elements:
list_with_list_comprehension_and_nested_lists_in_reverse_order = [[i for i in range(3, 0, -1)], [i for i in range(6, 3, -1)]]
print(list_with_list_comprehension_and_nested_lists_in_reverse_order)list_with_list_comprehension_and_nested_lists_in_reverse_order = [[i for i in range(3, 0, -1)], [i for i in range(6, 3, -1)]]
print(list_with_list_comprehension_and_nested_lists_in_reverse_order)Output:
C:\Users\username>python list_with_list_comprehension_and_nested_lists_in_reverse_order.py
[[3, 2, 1], [6, 5, 4]]C:\Users\username>python list_with_list_comprehension_and_nested_lists_in_reverse_order.py
[[3, 2, 1], [6, 5, 4]]In this example, we declare a list with a list comprehension and nested lists in reverse order and assign it to the variable list_with_list_comprehension_and_nested_lists_in_reverse_orderlist_with_list_comprehension_and_nested_lists_in_reverse_order. We then print the list to the console. The output shows that the list contains two lists as elements. This is also known as a two-dimensional list.
Types of List
Lists in Python can be categorized into the following types:
- Mutable Lists: Lists whose elements can be modified after creation.
- Immutable Lists: Lists whose elements cannot be modified after creation.
- Nested Lists: Lists that contain other lists as elements.
- Empty Lists: Lists that contain no elements.
- Singleton Lists: Lists that contain a single element.
- Homogeneous Lists: Lists that contain elements of the same data type.
- Heterogeneous Lists: Lists that contain elements of different data types.
- Ordered Lists: Lists whose elements are stored in a particular order.
- Unordered Lists: Lists whose elements are not stored in a particular order.
- Sorted Lists: Lists whose elements are stored in ascending or descending order.
- Unsorted Lists: Lists whose elements are not stored in ascending or descending order.
Mutable Lists
Lists in Python are mutable, meaning their elements can be modified after creation. For example, the following code modifies the first element of a list:
mutable_list = [1, 2, 3, 4, 5]
mutable_list[0] = 6
print(mutable_list)mutable_list = [1, 2, 3, 4, 5]
mutable_list[0] = 6
print(mutable_list)Output:
C:\Users\username>python mutable_list.py
[6, 2, 3, 4, 5]C:\Users\username>python mutable_list.py
[6, 2, 3, 4, 5]In this example, we declare a list with five elements and assign it to the variable mutable_listmutable_list. We then modify the first element of the list and print the list to the console. The output shows that the first element of the list has been modified.
Immutable Lists
Lists in Python are immutable, meaning their elements cannot be modified after creation. For example, the following code attempts to modify the first element of a list:
immutable_list = (1, 2, 3, 4, 5)
immutable_list[0] = 6
print(immutable_list)immutable_list = (1, 2, 3, 4, 5)
immutable_list[0] = 6
print(immutable_list)Output:
C:\Users\username>python immutable_list.py
Traceback (most recent call last):
File "immutable_list.py", line 2, in <module>
immutable_list[0] = 6
TypeError: 'tuple' object does not support item assignmentC:\Users\username>python immutable_list.py
Traceback (most recent call last):
File "immutable_list.py", line 2, in <module>
immutable_list[0] = 6
TypeError: 'tuple' object does not support item assignmentIn this example, we declare a list with five elements and assign it to the variable immutable_listimmutable_list. We then attempt to modify the first element of the list and print the list to the console. The output shows that the first element of the list cannot be modified.
Nested Lists
Lists in Python can contain other lists as elements. For example, the following code declares a nested list:
nested_list = [[1, 2, 3], [4, 5, 6]]
print(nested_list)nested_list = [[1, 2, 3], [4, 5, 6]]
print(nested_list)Output:
C:\Users\username>python nested_list.py
[[1, 2, 3], [4, 5, 6]]C:\Users\username>python nested_list.py
[[1, 2, 3], [4, 5, 6]]In this example, we declare a nested list and assign it to the variable nested_listnested_list. We then print the list to the console. The output shows that the list contains two lists as elements. This is also known as a two-dimensional list.
Empty Lists
Lists in Python can be empty, meaning they contain no elements. For example, the following code declares an empty list:
empty_list = []
print(empty_list)empty_list = []
print(empty_list)Output:
C:\Users\username>python empty_list.py
[]C:\Users\username>python empty_list.py
[]In this example, we declare an empty list and assign it to the variable empty_listempty_list. We then print the list to the console. The output shows that the list is empty.
Singleton Lists
Lists in Python can contain a single element. For example, the following code declares a list with a single element:
singleton_list = [1]
print(singleton_list)singleton_list = [1]
print(singleton_list)Output:
C:\Users\username>python singleton_list.py
[1]C:\Users\username>python singleton_list.py
[1]In this example, we declare a list with a single element and assign it to the variable singleton_listsingleton_list. We then print the list to the console. The output shows that the list contains a single element.
Homogeneous Lists
Lists in Python can contain elements of the same data type. For example, the following code declares a list with five elements of the same data type:
homogeneous_list = [1, 2, 3, 4, 5]
print(homogeneous_list)homogeneous_list = [1, 2, 3, 4, 5]
print(homogeneous_list)Output:
C:\Users\username>python homogeneous_list.py
[1, 2, 3, 4, 5]C:\Users\username>python homogeneous_list.py
[1, 2, 3, 4, 5]In this example, we declare a list with five elements of the same data type and assign it to the variable homogeneous_listhomogeneous_list. We then print the list to the console. The output shows that the list contains five elements of the same data type.
Heterogeneous Lists
Lists in Python can contain elements of different data types. For example, the following code declares a list with four elements of different data types:
heterogeneous_list = [1, 2.0, "three", [4, 5]]
print(heterogeneous_list)heterogeneous_list = [1, 2.0, "three", [4, 5]]
print(heterogeneous_list)Output:
C:\Users\username>python heterogeneous_list.py
[1, 2.0, 'three', [4, 5]]C:\Users\username>python heterogeneous_list.py
[1, 2.0, 'three', [4, 5]]In this example, we declare a list with four elements of different data types and assign it to the variable heterogeneous_listheterogeneous_list. We then print the list to the console. The output shows that the list contains four elements of different data types.
Ordered Lists
Lists in Python are ordered, meaning their elements are stored in a particular order. It can’t be changed. If you add elements to the list, it will be added at the end of the list. For example, the following code declares a list with five elements in a particular order:
ordered_list = [1, 2, 6, 9, 5]
print(ordered_list)ordered_list = [1, 2, 6, 9, 5]
print(ordered_list)Output:
C:\Users\username>python ordered_list.py
[1, 2, 6, 9, 5]C:\Users\username>python ordered_list.py
[1, 2, 6, 9, 5]In this example, we declare a list with five elements in a particular order and assign it to the variable ordered_listordered_list. We then print the list to the console. The output shows that the list contains five elements in a particular order.
Unordered Lists
Lists in Python are unordered, meaning their elements are not stored in a particular order. It can be changed automatically. For example, the following code declares a list with five elements in no particular order:
unordered_list = {1, 2, 6, 9, 5}
print(unordered_list)unordered_list = {1, 2, 6, 9, 5}
print(unordered_list)Output:
C:\Users\username>python unordered_list.py
{1, 2, 6, 9, 5}C:\Users\username>python unordered_list.py
{1, 2, 6, 9, 5}In this example, we declare a list with five elements in no particular order and assign it to the variable unordered_listunordered_list. We then print the list to the console. The output shows that the list contains five elements in no particular order.
Sorted Lists
Lists in Python can be sorted in ascending or descending order. For example, the following code declares a list with five elements in ascending order:
sorted_list = [1, 2, 6, 9, 5]
sorted_list.sort()
print(sorted_list)sorted_list = [1, 2, 6, 9, 5]
sorted_list.sort()
print(sorted_list)Output:
C:\Users\username>python sorted_list.py
[1, 2, 5, 6, 9]C:\Users\username>python sorted_list.py
[1, 2, 5, 6, 9]In this example, we declare a list with five elements in ascending order and assign it to the variable sorted_listsorted_list. We then print the list to the console. The output shows that the list contains five elements in ascending order.
Unsorted Lists
Lists in Python can be unsorted in ascending or descending order. For example, the following code declares a list with five elements in unsorted order:
unsorted_list = [1, 2, 6, 9, 5]
print(unsorted_list)unsorted_list = [1, 2, 6, 9, 5]
print(unsorted_list)Output:
C:\Users\username>python unsorted_list.py
[1, 2, 6, 9, 5]C:\Users\username>python unsorted_list.py
[1, 2, 6, 9, 5]In this example, we declare a list with five elements in unsorted order and assign it to the variable unsorted_listunsorted_list. We then print the list to the console. The output shows that the list contains five elements in unsorted order.
Conclusion
In this guide, we explored the ins and outs of Python lists, covering creation, manipulation, iteration, and best practices. We also covered the properties of lists and how to declare lists in Python. Now that you have a solid understanding of lists, you can start using them in your Python programs.
Try it: Python List Exercises
Exercise 1 – Create and Print a List
Exercise 2 – List Length and Indexing
Exercise 3 – Nested List
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
