
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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"]
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
-
'cherry' 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.
list= [5,2,9,7,5,8,1,4,3] print(list[2]) print(list[5])
The above code produces the following results:
9 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 index or position.
Example
The following is an example code to show the negative indexing of lists.
list= [5,2,9,7,5,8,1,4,3] print(list[-2]) print(list[-8])
When you run the program, it will show this output-
4 2
Slicing List
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 is as follows.
List[Start : Stop : Stride]
The above expression returns the portion of the list from index Start to index Stop, at a step size Stride.
Example 1
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.
list= [5,2,9,7,5,8,1,4,3] print(list[0:6]) print(list[1:9:2]) print(list[-1:-5:-2])
Output
After running the program, you will get this result:
[5, 2, 9, 7, 5, 8] [2, 7, 8, 4] [3, 1]