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. Following is a list of integer values.
lis= [1,2,3,4,5] print[lis]
If you execute the above snippet, produces the following output.
[1, 2, 3, 4, 5]
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.
They are two types of indexing −
- Positive Indexing
- Negative Indexing
Positive Indexing
In positive the first element of the list is at an index of 0 and the following elements are at +1 and as follows.
In the below figure, 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]]
Output
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 at -1 and the previous element at -2 and goes on till the first element.
In the below figure, 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]]
Output
The above code produces the following results
4 2
Slicing List
List slicing is a frequent practice in Python, and it is the most prevalent technique used by programmers to solve efficient 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 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
In the following example we have used the slice operation to slice a list. We also use 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
The above code produces the following results
[5, 2, 9, 7, 5, 8] [2, 7, 8, 4] [3, 1]
Updated on 05-Sep-2022 08:30:02
- Related Questions & Answers
- How to index and slice a tuple in Python?
- How to Create and Assign Lists in Python?
- Python - Find starting index of all Nested Lists
- Find minimum of each index in list of lists in Python
- How to compare two lists in Python?
- Minimum Index Sum of Two Lists in C++
- Empty Slice vs. Nil Slice in Golang
- How to join list of lists in python?
- How To Do Math With Lists in python ?
- Program to find cost to reach final index of any of given two lists in Python
- Push and slice multiple times in MongoDB?
- Python Program to Merge Two Lists and Sort it
- Updating Lists in Python
- In Python how to create dictionary from two lists?
- How do we define lists in Python?