Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct syntax for nested list or set comprehension

Imagine this object:

my_obj = {
    'Episodes' : [
        {'Tags' : ['one','two','three']},
        {'Tags' : ['three','four','five']}
            ]
        }

I want to create a set of the tags:

tags = set(tag for tag in e['Tags'] for e in my_obj['Episodes'])

However, it doesn't work because e is not defined. How can I do it??

like image 262
MFB Avatar asked Apr 23 '26 18:04

MFB


1 Answers

tags = set(tag  for e in my_obj['Episodes'] for tag in e['Tags'])

you need to change the order and get e first... :)

like image 175
Joran Beasley Avatar answered Apr 26 '26 07:04

Joran Beasley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!