Multiply only the integers in a mixed list in Python

Updated

Sometimes, while working with Python, we can come across a problem in which we require to find the product of list. This problem is easier to solve. But this can get complex in case we have mixture of data types to go along with it. Let’s discuss certain ways in which this task can be performed.

Method #1 : 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 added to product and hence can solve the problem.

# Python3 code to demonstrate working of 
# Mixed List Multiplication
# using type caste and exception handling 

# initializing list 
test_list = [5, 6, "w3c", 8, (5, 7), 'is', 9] 

# printing original list 
print("The original list is : " + str(test_list)) 

# Mixed List Multiplication
# using type caste and exception handling 
res = 1
for ele in test_list: 
	try: 
		res *= int(ele) 
	except : 
		pass

# printing result 
print("Product of integers in list : " + str(res)) 
Output : 

The original list is : [5, 6, 'w3c', 8, (5, 7), 'is', 9]
Product of integers in list : 2160

Method #2 : Using loop + isinstance() :

This problem can also be solved using the inbuilt function of loop and it also supports the instance filter using isinstance() which can be feeded with integer and hence solve the problem.

# Python3 code to demonstrate working of 
# Mixed List Multiplication
# using loop + isinstance() 

# getting Product 
def prod(val) : 
    res = 1 
    for ele in val: 
        res *= ele 
    return res  

# initializing list 
test_list = [5, 6, "w3c", 8, (5, 7), 'is', 9] 

# printing original list 
print("The original list is : " + str(test_list)) 

# Mixed List Multiplication
# using loop + isinstance() 
res = prod(filter(lambda i: isinstance(i, int), test_list)) 

# printing result 
print("Product of integers in list : " + str(res)) 
Output : 

The original list is : [5, 6, 'w3c', 8, (5, 7), 'is', 9]
Product of integers in list : 2160
Comment
Next tutorial
Mithlesh Upadhyay Published 26 Nov 2026 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials