Python Forum

Full Version: Type Error: Unsupported Operand
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings! I am new to this group and apologize in advance if I'm not asking for help properly.

I'm trying to figure out this error:

TypeError: unsupported operand type(s) for -: 'int' and 'list'

For this part of the code:
i = 0.0
for field in val_group_fields:
mprop = [float(sumdict[field]) / (float(sumdict["Total"]))]
i+= mprop * (1 - mprop)
sumdict[field] = [sumdict[field], mprop]

The error happens because of the + and – operators for the mprop list. It looks like mprop is either an integer or list and they cannot be added or subtracted together. I think I have to change them to work, but I can’t figure out how to do that. Please assist. Thank you!
Greetings and felicitations!

Why not show us what val_group_fields and sumdict look like? Maybe also explain exactly what you wish to achieve?

You can't add an integer to a list.
You can append an integer to a list. Is that what you want to do?
You can remove an element from a list. Is that what you want to do?
You can add a list to a list. Is that what you want to do?
You can divide an integer by a floating point number and vice-versa.

field is presumably an integer.

Quote:sumdict[field]) / (sumdict["Total"]

will give a number, probably not an integer, probably less than 1.

I can't see what this is for:

Quote:i+= mprop * (1 - mprop)

What should it achieve?
Thank you very much, Pedroski55! I will give this a try and comment back any additional errors as a result.

Kind regards,
jhancock