Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to index and slice lists in Python?
Lists are one of the four most commonly used data structures provided by Python. A list is a data structure in Python that is mutable and has an ordered sequence of elements. In Python, a list is like a box in which you can keep many things like numbers, names, or your favorite fruits. Let us say you have a list like this ?
fruits = ["apple", "banana", "mango"] print(fruits)
['apple', 'banana', 'mango']
Here, each fruit has a position in the list. But in Python, we start counting from 0, not 1. So here is the indexing of apple, banana, and mango −
-
'apple' is at position 0
-
'banana' is at position 1
-
'mango' is at position 2
Now, if you want more than one item from the list, then you can use the slicing method. This means picking a part of the list with the help of start and end positions.
In this article, we will discuss how to index and slice lists in Python.
Indexing Lists
In Python, every list with elements has a position or index. Each element of the list can be accessed or manipulated by using the index number.
There are two types of indexing −
- Positive Indexing
- Negative Indexing
Positive Indexing
In positive indexing, the first element of the list is at an index of 0, and the following elements are at +1 and so on. In the figure below, we can see how an element is associated with its index or position.
Example
The following is an example code to show positive indexing of lists ?
numbers = [5, 2, 9, 7, 5, 8, 1, 4, 3]
print("Element at index 2:", numbers[2])
print("Element at index 5:", numbers[5])
Element at index 2: 9 Element at index 5: 8
Negative Indexing
In negative indexing, the indexing of elements starts from the end of the list. That is, the last element of the list is said to be at a position of -1 and the previous element at -2, and goes on till the first element.
In the figure below, we can see how an element is associated with its negative index or position.
Example
The following is an example code to show the negative indexing of lists ?
numbers = [5, 2, 9, 7, 5, 8, 1, 4, 3]
print("Element at index -2:", numbers[-2])
print("Element at index -8:", numbers[-8])
Element at index -2: 4 Element at index -8: 2
Slicing Lists
List slicing is a frequent practice in Python, and it is the most prevalent technique used by programmers to solve problems. Consider a Python list. You must slice a list in order to access a range of elements in it. One method is to utilize the colon as a simple slicing operator (:).
The slice operator allows you to specify where to begin slicing, where to stop slicing, and what step to take. List slicing creates a new list from an old one.
Syntax
The syntax for the list slicing is as follows ?
List[Start : Stop : Step]
The above expression returns the portion of the list from index Start to index Stop (exclusive), at a step size of Step.
Basic Slicing Examples
In the following example, we have used the slice operation to slice a list. We also use the negative indexing method to slice a list ?
numbers = [5, 2, 9, 7, 5, 8, 1, 4, 3]
print("Slice [0:6]:", numbers[0:6])
print("Slice [1:9:2]:", numbers[1:9:2])
print("Slice [-1:-5:-2]:", numbers[-1:-5:-2])
Slice [0:6]: [5, 2, 9, 7, 5, 8] Slice [1:9:2]: [2, 7, 8, 4] Slice [-1:-5:-2]: [3, 1]
Common Slicing Patterns
Here are some commonly used slicing patterns ?
fruits = ['apple', 'banana', 'mango', 'orange', 'grape']
# Get first three elements
print("First 3:", fruits[:3])
# Get last two elements
print("Last 2:", fruits[-2:])
# Get all elements except first and last
print("Middle:", fruits[1:-1])
# Reverse the list
print("Reversed:", fruits[::-1])
First 3: ['apple', 'banana', 'mango'] Last 2: ['orange', 'grape'] Middle: ['banana', 'mango', 'orange'] Reversed: ['grape', 'orange', 'mango', 'banana', 'apple']
Conclusion
List indexing allows you to access individual elements using positive (0-based) or negative (-1 from end) indices. List slicing uses the [start:stop:step] syntax to extract portions of a list, creating powerful ways to manipulate data sequences in Python.
