| by Arround The Web | No comments

How to Get a Position of Element in Python?

Elements” are referred to as building blocks that represent single values in the data structure including strings, list, and set. Various operations, such as access location, manipulation, and elimination are performed in the programming language on particular elements of a given data set. Python also supports these features and one of them is to get the exact position of an element within the given data. For this particular purpose, different built-in methods/functions are used in Python.

In the guide, we will illustrate getting the position of an element in Python.

How to Find an Index of the Element in Python?

Python provides different situations where you must know the exact placement of a particular element within a list or string. Python has multiple built-in methods and functions for performing comparable tasks.

Let’s go through a few examples of how to retrieve the index of items using various methods.

Example 1: Get the Position of an Element Using the “index()” Method from the Python List
The “index()” method integrated into Python provides a basic and straightforward technique to discover the location of an element in a list. If the element exists, it delivers the index of its initial occurrence otherwise, it throws a ValueError.

First, declare a list variable named “mylist” and then, initialize it with the integer values. After that, create the “find_element” variable and assign it a desired element of the previously declared list:

mylist = [100, 200, 300, 400, 50]
find_element = 300

Now, invoke the “index()” method that takes the “find_element” variable as an argument to get its index and pass it to the “position1” variable inside the “try” block. Then, apply the “print()” statement to get the resultant value. If the specified element is not found in the list, it will display the “ValueError”:

try:
    position1 = mylist.index(find_element)
    print(f"The Element {find_element} is at Position {position1}.")
except ValueError:
    print("Element not found in the Given list.")

According to the below-given output, the position of the specified element is “2”:

Method 2: Get the Index/Position of an Element Using the “find()” Method from Python String
Python includes the “find()” method for locating a substring within a string. It returns the position of the substring’s first occurrence. The main distinction is that “find()” returns “-1” if the input substring is not found.

Let’s move towards a simple example.

First of all, declare the “mystring“ variable and initialize it with the string. Then, declared another variable “sub_string” and pass it to the substring:

mystring = "Today is a Good Day"
sub_string = "Good"

find_position = mystring.find(sub_string)

After that, use the “if else” condition to check the position of the provided substring and call the “print()” statement to display the position of the string:

if find_position != -1:
    print(f"Given Substring '{sub_string}' is at the Position {find_position}.")
else:
    print("Substring not found in the string.")

Output

We have compiled detailed information on how to get a position of an element in Python.

Conclusion

Python has various techniques to retrieve the position of an element including “index()” methods as well as the “find()” method for getting the substring index value. This guide has demonstrated how to get the position of an element in a particular type of data in Python.

Share Button

Source: linuxhint.com

Leave a Reply