Python is one of the most popular programming languages today for various reasons. It is an easy language to learn and use with a lot of libraries that make it easier to do different tasks. Learn how to get the list of indexes of a repeated python list item in this article.
What is the Repeated Python List Item?
If you have a list of numbers and you want to find the indexes of all the items that are repeated, you can use the Python list index() method. The index() method takes an element as an argument and returns the first index of that element in the list. If the element is not present in the list, it raises a value error exception.
To get the list of indexes of all the items that are repeated in a Python list, you can use a for loop. In each iteration of the loop, you can check if the current item is in the list more than once. If it is, you can add its indexes to list one by one until we get value error(means no elements are left).
For example, let’s say you have a list of numbers like this:
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
You can use a for loop to iterate over the list and check if each item is repeated:
repeated_indexes = []
for i in range(len(numbers)):
if numbers[i] in numbers[i + 1:]:
repeated_indexes.append(i)
How to get the list of indexes of a repeated item in list ?
If you want to get the list of indexes of a repeated item in python list. There are various ways to do this, but one simple way is to use the enumerate() function.
Using Enumerate function
Here’s an example:
my_list = ['a', 'b', 'c', 'd', 'e', 'f']
for index, item in enumerate(my_list):
if item == 'c':
print(index)
output:
2
As you can see, this prints out the index (3) of the item ‘c’. You can also use the enumerate() function to get the list of indexes of all repeated items:
my_list = ['a', 'b', 'a', 'd', 'a', 'f']
for index, item in enumerate(my_list):
if item in my_list[:index]:
print(index)
output:
2
4
5
In this case, we’re using the in operator to check if an item is in the part of the list before its current index. This will return True for all repeated items except for the first instance of each repeat.
Using list.index() method
Another way to get the list of indexes of a duplicate items is to use the index function as we have discussed above. Here we can use the python list.index() method to get the first index value.
nums = [1,2,3,45,64,2,3]
ind = nums.index(3)
print(ind)
output: 2
And then we can run a loop till we find all the indexes for this particular key. Lets see the full code in action.
Here we have and list nums = [1,2,3,4,5,6,2,4,5] and we will find the indexes of repeated item 2 (“key = 2”). It will return the indexes of a repeated item 2.
# find all indexes of an item in list
def indexes_of_item(my_list, key):
index_val = []
i=0 #counter
while i != len(my_list):
try:
ind = my_list[i:len(my_list)].index(key) #checking the index
index_val.append(ind+i)
i = ind+1+i
except ValueError:
i = len(my_list)
return index_val
nums = [1,2,3,4,5,6,2,4,5]
key = 2
result = indexes_of_item(nums, key)
print(result)
Output: [1,6]
Conclusion
Repeating items in a list is a common occurrence in Python programming. Luckily, there are some ways to get the list of indexes of a repeated item as we have discussed above. Similarly, to get the count of occurrences of an repeated items in list is very frequent in python. we will discuss this issue in the next articles with lots of different ways.
In this article, we learnt how to use this method to find the list of indexes of a duplicate items in a Python list. Please let us know in the comment if you have any other interesting ways to get this indexex list.