Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGabriel Guerra
Data Analysis Techdegree Graduate 9,109 PointsTypeError: unsupported operand type(s) for &: 'set' and 'list'
HI there Any idea why I get this type error:
turkey = {'turkey', 'cheese'}
vegetarian = {'tomato', 'onion', 'mushrooms'}
denver = {'tomato', 'eggs'}
bacon = ['mushrooms', 'eggs']
shared_ingredients = vegetarian & bacon & denver
print(shared_ingredients)
Thanks, Gabriel
1 Answer
Jeff Muday
Treehouse Moderator 28,720 PointsPython is more strict when it comes to sets and operators. These have to be all sets in order to be syntactically correct.
Perhaps you meant this:
turkey = {'turkey', 'cheese'}
vegetarian = {'tomato', 'onion', 'mushrooms'}
denver = {'tomato', 'eggs'}
bacon = {'mushrooms', 'eggs'} # here as a set { }
shared_ingredients = vegetarian & bacon & denver
print(shared_ingredients)
This is also correct:
turkey = {'turkey', 'cheese'}
vegetarian = {'tomato', 'onion', 'mushrooms'}
denver = {'tomato', 'eggs'}
bacon = set( ['mushrooms', 'eggs'] ) # converts the list to a set
shared_ingredients = vegetarian & bacon & denver
print(shared_ingredients)
This will result in the null set because there isn't an intersection of vegetarian
, bacon
, and denver
.