List Indexing in Python
As a data scientist and a python developer, I have frequently used list indexing to access and modify elements within lists. List indexing is a fundamental concept for python developers, allowing for precise control over data manipulation. In this article, you will learn how to:
· Define and create lists in python
· Understand the concept of list indexing in python
· Effectively index lists in python
· Slice lists in python
What is a python list?
A list is a collection of elements, where each element is called an item or object. Python lists are typically ordered, meaning the items are arranged in a specific sequence. They are also mutable which allows you to change their contents without affecting their identity or memory location. Furthermore, python lists are heterogeneous (can store items of various data types) making them versatile for different applications.
How to create python lists
Python lists can be created in two main ways. The first method is using list literals. For example:
age_list = [10,20,25,30]
age_list
Output:
[10, 20, 25, 30]
The code above creates a list of people’s ages using list literal method, which involves enclosing the list items within square brackets [ ].
The second method of creating python lists is using the built-in list function, which has the syntax list(iterable). For example:
age_list = list([10,20,25,30])
age_list
fruit_list = list(("mango","apple","orange","bananas"))
fruit_list
string_list= list("hello world")
string_list
Output:
[10, 20, 25, 30]
['mango', 'apple', 'orange', 'bananas']
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
The above code creates an age list, fruit list from a tuple, and a string list from a string using the built-in list function.
What is list indexing in python?
List indexing in python is a technique used to access specific items within a list. It relies heavily on numerical positions assigned to each item called indexes. It is important to note that, python indexing starts at 0, meaning the first item resides at index 0, the second at index 1, and so on.
Example:
my_list = [1,5,3,7,8]
In the above example:
· The item 1 is at index 0 (first item).
· The item 5 is at index 1 (second item)
To access a specific item, we can use square brackets [ ]
Example:
number_seven = my_list[3]
number_seven
number_five = my_list[1]
number_five
Output:
5
7
Negative list indexing in python
Negative list indexing in python refers to the use of negative indexes to access items within a list. To access the last item of a list, you can simply use an index of -1. Subsequent elements can be accessed using progressively more negative indexes. Negative indexing in python starts from -1, meaning the last element resides at index -1. (The sequence moves from back to front)
Example:
my_list= [1,2,5,9,10]
my_list
number_10 = my_list[-1]
number_10
number_9 = my_list[-2]
number_9
Output:
[1, 2, 5, 9, 10]
10
9
In the above example, we created a simple list my_list and we simply accessed 10 and 9 from the list using negative indexes.
What is list slicing in python?
List slicing in python is the process of extracting a portion of a list. List slicing uses the syntax [ start : stop : step ], where:
· start is the index of the first item to include (default is usually 0)
· stop is the index of the first item to exclude (default is usually the length of the list)
· step is the interval between the items (default value is usually 1)
Example:
my_list = [1,2,3,4,5,6]
my_list
sliced_list = my_list[2:5:2]
sliced_list
Recommended by LinkedIn
Output:
[1, 2, 3, 4, 5, 6]
[3, 5]
The python code above extracts items from index 2 to 5 (exclusive) with a step of two.
Access all items from a python list
Example:
my_list = [1,2,3,4,5,6]
my_list
sliced_list = my_list[:]
sliced_list
Output:
[1, 2, 3, 4, 5, 6]
The above example uses the slicing operator colon (:) to obtain all the items in the list.
Access all the items from one position to another position within a python list.
Example:
my_list = [1,2,3,4,5,6]
my_list
sliced_list = my_list[2:5]
sliced_list
Output:
[1, 2, 3, 4, 5, 6]
[3, 4, 5]
In the above example we got items from position 2 to 5
Access all items after a specific position
Example:
my_list = [1,2,3,4,5,6]
my_list
sliced_list = my_list[2:]
sliced_list
Output:
my_list = [1,2,3,4,5,6]
my_list
sliced_list = my_list[2:]
sliced_list
The above code illustrates how to get elements from a python list after a specific position, in this case we obtained all elements after index 2.
Access all items before a specific position
Example:
my_list = [1,2,3,4,5,6]
my_list
sliced_list = my_list[:3]
sliced_list
Output:
[1, 2, 3, 4, 5, 6]
[1, 2, 3]
The above code illustrates how to get elements from a python list before a specific position, in this case we obtained all elements before index 2.
Access all the items from a python list at specified intervals
Example:
my_list = [1,2,3,4,5,6]
my_list
sliced_list = my_list[: :2]
sliced_list
Output:
[1, 2, 3, 4, 5, 6]
[1, 3, 5]
The above python code showcases how to get items from a list at an interval of two, notice how we have used double colon [: :]. If you want to index at an interval from the last element to the first, you can use negative indexing as shown below:
Python:
my_list = [1,2,3,4,5,6]
my_list
sliced_list = my_list[: :-2]
sliced_list
Output:
[1, 2, 3, 4, 5, 6]
[6, 4, 2]
Conclusion:
List indexing is a fundamental yet versatile technique in python programming. To fully grasp its intricacies and applications, it is essential to practice the concepts you have learnt. By actively experimenting with different list operations and scenarios, you can solidify your understanding and become more proficient in python programming.
So interesting, thanks Harron!