Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: unsupported operand type(s) for +: 'set' and 'set'

Tags:

python

I'm trying to learn python for data science application and signed up for a course. I am doing the exercises and got stuck even though my answer is the same as the one in the answer key.

I'm basically trying to add two new items to a dictionary with the following piece of code:

# Create a new key-value pair for 'Inner London'
location_dict['Inner London'] = location_dict['Camden'] + location_dict['Southwark']

# Create a new key-value pair for 'Outer London'
location_dict['Outer London'] = location_dict['Brent'] + location_dict['Redbridge']

but when I run it I am getting a TypeError.

like image 909
Meat Kitten Avatar asked Jan 16 '18 13:01

Meat Kitten


People also ask

How do I fix unsupported operand type s?

The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num .

What does unsupported operand mean?

The TypeError: unsupported operand type(s) for +: 'int' and 'str' error occurs when an integer value is added with a string that could contain a valid integer value. Python does not support auto casting. You can add an integer number with a different number. You can't add an integer with a string in Python.

What does unsupported operand type S for +: INT and STR mean?

The Python "TypeError: unsupported operand type(s) for +: 'int' and 'str'" occurs when we try to use the addition (+) operator with an integer and a string. To solve the error, convert the string to an integer, e.g. my_int + int(my_str) .


1 Answers

I had a list in there with the sets I think that was what was throwing the error I just converted the list to a set and joined them with .union. Thank you @Adelin, @Bilkokuya, @ Piinthesky, and @Kurast for you're quick responses and input!

like image 75
Meat Kitten Avatar answered Oct 19 '22 13:10

Meat Kitten