# Python: Find the indices of all occurrences of a given item in a given list

## Python Basic - 1: Exercise-109 with Solution

Write a Python program to find the indices of all occurrences of a given item in a given list.

**Sample Solution-1:**

**Python Code:**

```
def indices_in_list(nums_list, n):
return [idx for idx, i in enumerate(nums_list) if i == n]
nums = [1,2,3,4,5,2]
print("Original list of numbers:",nums)
n = 2
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
nums = [3,1,2,3,4,5,6,3,3]
print("\nOriginal list of numbers:",nums)
n = 3
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
nums = [1,2,3,-4,5,2,-4]
print("\nOriginal list of numbers:",nums)
n = -4
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
nums = [1,2,3,4,5,2]
print("\nOriginal list of numbers:",nums)
n = 7
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
```

Sample Output:

Original list of numbers: [1, 2, 3, 4, 5, 2] Given Number 2 Indices of all occurrences of the said item in the given list: [1, 5] Original list of numbers: [3, 1, 2, 3, 4, 5, 6, 3, 3] Given Number 3 Indices of all occurrences of the said item in the given list: [0, 3, 7, 8] Original list of numbers: [1, 2, 3, -4, 5, 2, -4] Given Number -4 Indices of all occurrences of the said item in the given list: [3, 6] Original list of numbers: [1, 2, 3, 4, 5, 2] Given Number 7 Indices of all occurrences of the said item in the given list: []

**Pictorial Presentation:**

**Flowchart:**

**Sample Solution-2:**

**Python Code:**

```
from itertools import count
def indices_in_list(nums_list, n):
return [(i) for i, j in zip(count(), nums_list) if j == n]
nums = [1,2,3,4,5,2]
print("Original list of numbers:",nums)
n = 2
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
nums = [3,1,2,3,4,5,6,3,3]
print("\nOriginal list of numbers:",nums)
n = 3
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
nums = [1,2,3,-4,5,2,-4]
print("\nOriginal list of numbers:",nums)
n = -4
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
nums = [1,2,3,4,5,2]
print("\nOriginal list of numbers:",nums)
n = 7
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
```

Sample Output:

Original list of numbers: [1, 2, 3, 4, 5, 2] Given Number 2 Indices of all occurrences of the said item in the given list: [1, 5] Original list of numbers: [3, 1, 2, 3, 4, 5, 6, 3, 3] Given Number 3 Indices of all occurrences of the said item in the given list: [0, 3, 7, 8] Original list of numbers: [1, 2, 3, -4, 5, 2, -4] Given Number -4 Indices of all occurrences of the said item in the given list: [3, 6] Original list of numbers: [1, 2, 3, 4, 5, 2] Given Number 7 Indices of all occurrences of the said item in the given list: []

**Flowchart:**

**Sample Solution-3:**

**Python Code:**

```
from more_itertools import locate
def indices_in_list(nums_list, n):
return list(locate(nums_list, lambda x: x == n))
nums = [1,2,3,4,5,2]
print("Original list of numbers:",nums)
n = 2
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
nums = [3,1,2,3,4,5,6,3,3]
print("\nOriginal list of numbers:",nums)
n = 3
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
nums = [1,2,3,-4,5,2,-4]
print("\nOriginal list of numbers:",nums)
n = -4
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
nums = [1,2,3,4,5,2]
print("\nOriginal list of numbers:",nums)
n = 7
print("Given Number",n)
print("Indices of all occurrences of the said item in the given list:")
print(indices_in_list(nums, n))
```

Sample Output:

Original list of numbers: [1, 2, 3, 4, 5, 2] Given Number 2 Indices of all occurrences of the said item in the given list: [1, 5] Original list of numbers: [3, 1, 2, 3, 4, 5, 6, 3, 3] Given Number 3 Indices of all occurrences of the said item in the given list: [0, 3, 7, 8] Original list of numbers: [1, 2, 3, -4, 5, 2, -4] Given Number -4 Indices of all occurrences of the said item in the given list: [3, 6] Original list of numbers: [1, 2, 3, 4, 5, 2] Given Number 7 Indices of all occurrences of the said item in the given list: []

**Flowchart:**

**Python Code Editor:**

**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**

**Previous:**Write a Python program that takes three integers and check whether the sum of the last digit of first number and the last digit of second number equal to the last digit of third number.

**Next:** Write a Python program to remove the duplicate numbers from a given list of numbers.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join