Python | Multiply Integer in Mixed List of string and numbers
Last Updated :
24 Feb, 2025
We are given a list containing mixed data types: integers, strings, and even tuples. Our task is to multiply the integers present in the list, while ignoring the non-integer elements and return the final result. For example, if we have a list like this: `[5, 8, "gfg", 8, (5, 7), 'is', 2]`, and we want to multiply the integers, the output will be `640`as product of (5, 8, 8, and 2) is 640.
Using Type casting + Exception Handling
We can employ a brute force method to type caste each element and catch the exception if any occurs. This can ensure that only integers are multiplied with the product and hence can solve the problem.
Python
a = [5, 8, "gfg", 8, (5, 7), 'is', 2]
res = 1
for element in a:
try:
res *= int(element)
except:
pass
print(str(res))
Explanation:
- Iterates through a mixed list, initializing res to 1.
- Uses a try-except block to convert elements to integers and multiply them.
- Ignores non-numeric values and prints the final product.
Using isinstance()
This brute-force approach multiplies only the integer elements in a mixed-data list by iterating through all elements and using isinstance() to filter integers before multiplying.
Python
a = [5, 8, "gfg", 8, (5, 7), 'is', 2]
res = 1
for element in a:
if(isinstance(element, int)):
res *= element
print(str(res))
Explanation: isinstance(element, int) method is used to filter elements that are of integer type.
Using type()
We can loop through the list and multiply only the elements that are of integer type using type().
Python
a = [5, 8, "gfg", 8, (5, 7), 'is', 2]
res = 1
for element in a:
if(type(element) is int):
res *= element
print(str(res))
Explanation: operation res *= element is executed only for integer elements.
Using math.prod()
Math library in python provides .prod() method which can be used to get the product of all the elements in the list.
Python
import math
a = [5, 8, "gfg", 8, (5, 7), 'is', 2]
res = math.prod([x for x in a if isinstance(x, int)])
print(res)
Explanation: math.prod() multiplies all elements in the filtered list.
Using reduce()
We can use reduce() to multiply elements from a filtered list that contains only integers. The function starts with 1 and multiplies each integer iteratively.
Python
from functools import reduce
a = [5, 8, "gfg", 8, (5, 7), 'is', 2]
res = reduce(lambda x, y: x * y, [x for x in a if isinstance(x, int)], 1)
print(res)
Explanation: list comprehension [x for x in a if isinstance(x, int)] extracts integers and then reduce(lambda x, y: x * y, [...], 1) starts with 1 and multiplies all elements in the list.
Similar Reads
Python | Convert numeric String to integers in mixed List Sometimes, while working with data, we can have a problem in which we receive mixed data and need to convert the integer elements in form of strings to integers. This kind of operation might be required in data preprocessing step. Let's discuss certain ways in which this task can be performed. Metho
11 min read
Python | Convert list of numerical string to list of Integers Many times, the data we handle might not be in the desired form for any application and has to go through the stage of preprocessing. One such kind of form can be a number in the form of a string that too is a list in the list and we need to segregate it into digit-separated integers. Let's discuss
5 min read
Insert a number in string - Python We are given a string and a number, and our task is to insert the number into the string. This can be useful when generating dynamic messages, formatting output, or constructing data strings. For example, if we have a number like 42 and a string like "The number is", then the output will be "The num
2 min read
Python - Convert List of Integers to a List of Strings We are given a list of integers and our task is to convert each integer into its string representation. For example, if we have a list like [1, 2, 3] then the output should be ['1', '2', '3']. In Python, there are multiple ways to do this efficiently, some of them are: using functions like map(), re
3 min read
Python - Convert Number to List of Integers We need to split a number into its individual digits and represent them as a list of integers. For instance, the number 12345 can be converted to the list [1, 2, 3, 4, 5]. Let's discuss several methods to achieve this conversion.Using map() and str() Combination of str() and map() is a straightforwa
3 min read