Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the position of number that is multiple of certain number
When working with lists in Python, you often need to find the positions (indices) of numbers that are multiples of a specific number. Python provides several approaches to accomplish this task efficiently using loops, list comprehension, and built-in functions.
Algorithm
Define a list of numbers
Iterate through the list and find numbers that are multiples of the desired number
Store the positions of the multiples in a separate list
Using List Comprehension
List comprehension provides a concise way to find positions of multiples ?
numbers = [2, 4, 6, 8, 10, 12, 14]
multiple = 3
positions = [index for index, number in enumerate(numbers) if number % multiple == 0]
print("Numbers:", numbers)
print("Finding multiples of:", multiple)
print("Positions of multiples:", positions)
Numbers: [2, 4, 6, 8, 10, 12, 14] Finding multiples of: 3 Positions of multiples: [2, 5]
In this example, 6 (at index 2) and 12 (at index 5) are multiples of 3.
Using For Loop
A traditional for loop approach gives you more control over the process ?
numbers = [15, 20, 25, 30, 35, 40]
multiple = 5
positions = []
for index, number in enumerate(numbers):
if number % multiple == 0:
positions.append(index)
print("Numbers:", numbers)
print("Finding multiples of:", multiple)
print("Positions of multiples:", positions)
Numbers: [15, 20, 25, 30, 35, 40] Finding multiples of: 5 Positions of multiples: [0, 1, 2, 3, 4, 5]
Using Filter with Enumerate
The filter function can be combined with enumerate for a functional programming approach ?
numbers = [7, 14, 21, 28, 35, 42]
multiple = 7
# Using filter to find indices where numbers are multiples
indexed_numbers = enumerate(numbers)
filtered_positions = filter(lambda x: x[1] % multiple == 0, indexed_numbers)
positions = [index for index, value in filtered_positions]
print("Numbers:", numbers)
print("Finding multiples of:", multiple)
print("Positions of multiples:", positions)
Numbers: [7, 14, 21, 28, 35, 42] Finding multiples of: 7 Positions of multiples: [0, 1, 2, 3, 4, 5]
Handling Edge Cases
When no multiples are found, the result should be an empty list ?
numbers = [1, 3, 5, 7, 9, 11]
multiple = 2
positions = [index for index, number in enumerate(numbers) if number % multiple == 0]
print("Numbers:", numbers)
print("Finding multiples of:", multiple)
print("Positions of multiples:", positions)
print("No multiples found!" if not positions else f"Found {len(positions)} multiples")
Numbers: [1, 3, 5, 7, 9, 11] Finding multiples of: 2 Positions of multiples: [] No multiples found!
Comparison of Methods
| Method | Readability | Performance | Best For |
|---|---|---|---|
| List Comprehension | High | Fast | Simple filtering |
| For Loop | High | Good | Complex logic |
| Filter Function | Medium | Good | Functional programming |
Applications
Finding positions of multiples has various practical applications ?
Time series analysis finding data points at regular intervals
Data processing identifying records that meet divisibility criteria
Algorithm optimization avoiding unnecessary computations for non-multiples
Pattern recognition detecting regular sequences in data
Conclusion
List comprehension is the most Pythonic approach for finding positions of multiples, offering clean syntax and good performance. Use for loops when you need more complex logic or want to process results immediately as they're found.
---