user19589852
user19589852

Reputation:

Not all arguments converted during string formatting. Why am I having error

l=[[1,2,3,4], (2,3,4,5,6,), (3,4,5,6,7), set([23,4,5,45,4,4,5,45,45,4,5]), {'K1': "sudh", "k2": "ineuron", "k3":"kumar", 3:6,7:8}, ["ineuron", "datasience"]]
for i in l:
    if type(i)==list:
        for j in i:
            if j % 2!=0:
                print(j)

Why am I having error "not all arguments converted during string formatting"?

Upvotes: 0

Views: 101

Answers (2)

You've to check if the list contains strings because you can't divide strings.

l=[[1,2,3,4], (2,3,4,5,6,), (3,4,5,6,7), set([23,4,5,45,4,4,5,45,45,4,5]), {'K1': "sudh", "k2": "ineuron", "k3":"kumar", 3:6,7:8}, ["ineuron", "datasience"]]
for i in l:
    if type(i)==list:
        for j in i:
            if isinstance(j, str) == False and j % 2!=0:
                print(j) 

Upvotes: 1

Tim Shidlovskii
Tim Shidlovskii

Reputation: 124

You are trying to perform a modulo operation on a string:

"ineuron" % 2

The last element of l list is ["ineuron", "datasience"]. It passes the if statement, and then j becomes "ineuron" and you get an error.

Upvotes: 2

Related Questions