Meat Kitten
Meat Kitten

Reputation: 341

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

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.

Upvotes: 16

Views: 26705

Answers (2)

Ryabchenko Alexander
Ryabchenko Alexander

Reputation: 12370

for union of sets you can use |

location_dict['Camden'] | location_dict['Southwark']

Upvotes: 7

Meat Kitten
Meat Kitten

Reputation: 341

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!

Upvotes: 6

Related Questions