Python Find in List – How to Find the Index of an Item in a List (with example)

Find the Index of an Item in a List (with example)

To Find the Index of an Item in a List, We can use the index() function, Which takes a single value as an argument and return the index (offset) of the value from the list

There are multiple ways to find an index of the list

  • Using Index() function
  • Using Loop

Quick Overview of the List

The list is a data structure, A well-organized way of storing data, We have seen multiple examples of declaring a variable like a=”hello” this is to store a single piece of data. Let’s say, You wanted to store multiple pieces of data which has some connection with each other

For example – Storing all the names of states in the US in an order Can be easily achieved by a list

states_of_america = ["Delaware", "Pennsylvania", "New Jersey", "Georgia", "Connecticut", "Massachusetts", "Maryland", "South Carolina", "New Hampshire", "Virginia", "New York", "North Carolina", "Rhode Island", "Vermont", "Kentucky", "Tennessee", "Ohio", "Louisiana", "Indiana", "Mississippi", "Illinois", "Alabama", "Maine", "Missouri", "Arkansas", "Michigan", "Florida", "Texas", "Iowa", "Wisconsin", "California", "Minnesota", "Oregon", "Kansas", "West Virginia", "Nevada", "Nebraska", "Colorado", "North Dakota", "South Dakota", "Montana", "Washington", "Idaho", "Wyoming", "Utah", "Oklahoma", "New Mexico", "Arizona", "Alaska", "Hawaii"]

In some cases, We might also want to have an order in your data server,

example – if you are storing all the people in a virtual Q, Then you want to keep a hold of the queue So that First come will be served First and last will be last served

This can be well achieved by the List (Same example)

How to access a specific value on the list

To access a specific value in a list, We need to know the offset, and can be used below

print(states_of_america[2])

states_of_america -> would be the variable name

2 -> Would be the index (offset)

So the offset will be counted from 0 -> 2 and in the above example the output would be

Output:

New Jersey

In the list, We can also use the negative index (Offset), Which will get the value in descending order

Example:

print(states_of_america[-2])

Alaska

To change a value in a list:

We can simply add the value below

states_of_america[2] = "New State"
print(states_of_america[2])

Output:

New State

To add value to the end of the list

To add a single item or value to an end of a list, We can use the append() function

states_of_america.append("newly added state")

How to Find the Index of an Item in a List

Using Index() function

Using the Index() function is the best and most effective way in finding a value from a list. The index () function takes a single value as input and will give the index of the value from the list

Example:

letter=input("enter a letter in alphabet")
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
 position = alphabet.index(letter)
 print("You letter is at " + str(position) + " position in an alphabet")

NOTE: Code designed to be compatible with online editor to test it

Try it Yourself

Output:

enter a letter in alphabet j
You letter is at 9 position in an alphabet

Using LOOP

Just by iterating the values in the variable and finding the position of the matched value

Example:

letter=Input(“enter a letter in alphabet”)
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for position in range(len(alphabet)):
	if letter = alphabet[position]:
		print("You letter is at " + str(position) + " position in an alphabet")

Try it Yourself

Output:

enter a letter in alphabet j
You letter is at 9 position in an alphabet

Check here for Caesar Cipher example in python

Check here for more Python learning posts

Good Luck with your learning !!

Similar Posts