Explain indexing and slicing in list with example in python

In this python tutorial, we will discuss indexing and slicing in Python with a few examples and also we will cover these below topics:

  • What is Indexing in python?
  • Positive indexing example in Python
  • Negative indexing example in Python
  • Indexing in python string
  • Indexing in python list
  • Indexing in python array
  • Indexing in python tuple
  • Indexing in a python dictionary
  • What is slicing in python?
  • Python get substring using slice object
  • Python get substring using negative index
  • Slicing in python list
  • Slicing in python string
  • Slicing in python tuple
  • Slicing in python array

What is Indexing in python?

  • Indexing in Python means referring to an element of an iterable by its position within the iterable.
  • Each character can be accessed using their index number.
  • To access characters in a string we have two ways:
    • Positive index number
    • Negative index number

Positive indexing example in Python

In Python Positive indexing, we pass a positive index that we want to access in square brackets. The index number starts from 0 which denotes the first character of a string.

Example:

my_str = "Python Guides"
print(my_str[0])

In this output, we can see the positive indexing example in python. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Positive indexing example in Python

You may like to read, Python program to find sum of n numbers and How to add two numbers in Python.

Negative indexing example in Python

In negative indexing in Python, we pass the negative index which we want to access in square brackets. Here, the index number starts from index number -1 which denotes the last character of a string.

Example:

my_str = "Python Guides"
print(my_str[-1])

In this output, we can see a negative indexing example in python. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Negative indexing example in Python

String indexing in python allows you to access individual characters from the string directly by using the index. Here, “(str[3])” is used to get “c”.

Example:

str = 'Welcome'
print(str[3])

In this output, we can see indexing in python string. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Indexing in python string

You may like Python program to print element in an array.

Indexing in python list

Python List indexing can be done by accessing the list value by referring to its index number. Here, we have used “(list[2])” and it returns “Horse”.

Example:

list = ['Dog', 'Cat', 'Horse', 'Bear']
print(list[2])

In this output, we can see indexing in python list. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Indexing in python list

Indexing in Python array

Array indexing in python is the same as accessing an array element. Accessing an array element by referring to its index number. Here, we have used “(my_arr[1])” to access “12”.

Example:

import numpy as np
my_arr = np.array([10, 12, 14, 16])
print(my_arr[1])

In this output, we can see indexing in the python array. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Indexing in python array

Check out below Python array tutorials:

  • Python concatenate arrays
  • How to write Python array to CSV
  • Python shape of an array
  • How to Convert Python string to byte array with Examples
  • Python Array with Examples
  • Create an empty array in Python

Indexing in python tuple

We will use the index operator “[]” to access an item from a Python tuple. Where index starts from 0. Here, we have used a negative index which starts from the right “(my_tuple[-2])” to get “8”.

Example:

my_tuple = (2, 4, 6, 8, 10)
print(my_tuple[-2])

In this output, we can see indexing in python tuple. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Indexing in python tuple

  • Python convert tuple to list
  • Python sort list of tuples

Indexing in a python dictionary

Indexing a dictionary in Python is to accesses the key or value at that index. Here, we will use list() to index the key of a Python dictionary. To get key at index in the list we have used “m_list[1]” and it will return “John”.

Example:

my_dictionary = {"ani": 101, "John": 102}
m_list = list(my_dictionary)
k = m_list[1]
print(k)

In this output, we can see indexing in a python dictionary. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Indexing in a python dictionary

  • Python Dictionary Methods
  • Python dictionary append with examples
  • How to convert dictionary to JSON in Python

What is slicing in python?

Slicing in python is used for accessing parts of a sequence. The slice object is used to slice a given sequence or any object. We use slicing when we require a part of a string and not the complete string.

Syntax:

string[start : end : step]

Example:

my_str = "Python Guides"
print(my_str[-1 : -10 : -2])

In this output, we can see what is slicing in python. Here, the step is “-2” so from reverse every other value will be printed. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
What is slicing in python

Python get substring using slice object

To get the substring using slice object in python, we will use “slice(5)”. Here, start “0” and the end “5”, so it takes value one less than the end value.

Example:

my_str = 'Guides'
s_obj = slice(5) 
print(my_str[s_obj])

In this output, we can see how to get substring using slice object in python. Here, we can see the substring as “Guide”. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Python get substring using slice object

Python get substring using negative index

To get substring using negative index in python, we will use “slice(-1, -4, -1)”. Here, the start is “-1”, the end “-4”, and the step is negative “-1”.

Example:

my_str = 'Guides'
s_obj = slice(-1, -4, -1) 
print(my_str[s_obj])

In this output, we can see how to get substring using negative index in python. Here, we can see the substring as “sed”. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Python get substring using negative index

Slicing in Python list

Lets see slicing in python list.

In this example, we have used “(my_list[-4::1])” for slicing in the list. So, the start is “-4” and we have not specified any value for the end so, by default it will take till last and “1” is step.

Example:

my_list = [10, 20, 30, 40, 50, 60] 
print(my_list[-4::1]) 

In this output, we can see slicing in the python list. Here, we get the list in the output. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Slicing in python list

  • Check if a list exists in another list Python
  • Python write a list to CSV
  • Python list comprehension using if-else
  • Python select from a list
  • Python convert tuple to list
  • Python sort list of tuples

Slicing in python string

Slicing in python string is used for getting a substring from a given string by slicing it. We have specified the start index as “3”, and the end index as “6” and it will return a string.

Example:

str ='Python!'
print(str[3:6])

In this output, we can see slicing in a python string. Here, we get the string as an output. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Slicing in python string

  • How to convert an integer to string in python
  • Python convert list to string
  • String methods in Python with examples
  • Python generate random number and string
  • Python write String to a file

Slicing in python tuple

To slice a tuple in Python, we will use “(my_tuple[4])”. To get the value we have to specify a positive integer, to fetch that index from the tuple counting from left. In case of negative index, we have to fetch that index from the right.

Example:

my_tuple = ('v', 'w', 'x', 'y', 'z')
print(my_tuple[4])
print(my_tuple[-3])

In this output, we can see slicing in python tuple. Here, we get the two outputs. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Slicing in python tuple

  • Create a tuple in Python
  • Python convert tuple to list

Slicing in Python array

To do slicing in the python array, we will import numPy as np. We will define as “(my_arr[1:])”. Here, the start is “1” and the end is not passed so, it will consider till the last length of an array.

Example:

import numpy as np
my_arr = np.array([10, 11, 12, 13, 14])
print(my_arr[1:])

In this output, we can see slicing in the python array. Here, we get the output as an array. You can refer to the below screenshot.

Explain indexing and slicing in list with example in python
Slicing in python array

In this tutorial, we have learned about Indexing and slicing in Python with examples, and also we have covered these topics:

  • What is Indexing in python?
  • Positive indexing example in Python
  • Negative indexing example in Python
  • Indexing in python string
  • Indexing in python list
  • Indexing in python array
  • Indexing in python tuple
  • Indexing in a python dictionary
  • What is slicing in python?
  • Python get substring using slice object
  • Python get substring using negative index
  • Slicing in python list
  • Slicing in python string
  • Slicing in python tuple
  • Slicing in python array

Explain indexing and slicing in list with example in python

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.

What is list indexing and slicing with an example in Python?

“Indexing” means referring to an element of an iterable by its position within the iterable. “Slicing” means getting a subset of elements from an iterable based on their indices. By way of analogy, I was recently summoned to jury duty, and they assigned each potential juror a number.

What is indexing in Python with example?

The Python index() method helps you find the index position of an element or an item in a string of characters or a list of items. It spits out the lowest possible index of the specified element in the list. In case the specified item does not exist in the list, a ValueError is returned.

What is list slicing in Python with example?

It's possible to "slice" a list in Python. This will return a specific segment of a given list. For example, the command myList[2] will return the 3rd item in your list (remember that Python begins counting with the number 0).

What is indexing in list in Python?

Python index() is an inbuilt function in Python, which searches for a given element from the start of the list and returns the index of the first occurrence.