0

I would like to understand why is this a valid syntax:

common = (set(classes['Biology']) & set(classes['Math']) & set(classes['PE']) & set(classes['Social Sciences']) & set(classes['Chemistry']))

but not this:

common = set(classes['Biology']) & set(classes['Math']) & set(classes['PE'] & set(classes['Social Sciences']) & set(classes['Chemistry'])

TL;DR

Why is there a need to put all the unions into normal braces

()

Thank you.

divibisan
  • 11,659
  • 11
  • 40
  • 58
boldi
  • 83
  • 1
  • 6

1 Answers1

0

The second one is invalid because it's missing a close paren on set(classes['PE']. You don't need the outer parentheses, you just need to close the inner ones correctly.

Side-note: Performance-wise, you'd likely save a little by only explicitly converting the first item to a set, then using the intersection (which takes an arbitrary number of iterable arguments) to do the rest of the work in a single Python function call:

common = set(classes['Biology']).intersection(classes['Math'], classes['PE'], classes['Social Sciences'], classes['Chemistry'])
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Oh my god. I guess I have to be tired already. I've red those 2 codes like 5 times and I've seen them the same except outer braces... I've just copied it from a text file so syntax highlight wasn't available... – boldi May 31 '18 at 13:14
  • @boldi: Suggestion: Always use a syntax highlighting text editor. Even on Windows, lightweight stuff like Notepad++ supports syntax highlighting without needing to go to full fledged IDEs. Saves a *lot* of headaches. On Linux, with minor configuration, you likely have at least three syntax highlighting editors available, if not more. :-) – ShadowRanger May 31 '18 at 13:16
  • Yes. I am aware of the benefits of syntax highlight. This was just a exceptional situation, because I was on a PC that wasn't mine and wanted to do some Python learning in the meantime. – boldi May 31 '18 at 13:18