1

I have a python dict like so:

tt={'plot1':[
      {'var': 'ok__you', 'uuid': '98782098109'},
      {'var': 'hdg__akj', 'uuid': '712837901'}
      ], 
    'plot2': [
      {'var': 'ok__you2', 'uuid': '987820981092'},
      {'var': 'hdg__akj2', 'uuid': '7128379012'}
    ]}

I am trying to get all the uuids into one single list, so, I do:

lst=[v_i['uuid'] for v_i in value for key,value in tt.items()]

and I get thrown:

NameError: name 'value' is not defined

I have no clue why this is - I have specified the value in the tt.items() iteration - not sure why this error is being thrown?

Would apreciate any tips!

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
JohnJ
  • 6,736
  • 13
  • 49
  • 82

1 Answers1

2

You swapped the order of the two for loops

lst=[v_i['uuid'] for key,value in tt.items() for v_i in value]

The output will then be

['98782098109', '712837901', '987820981092', '7128379012']

Also since you are not using the key, you could just iterate on the values of the dictionaries

lst=[v_i['uuid'] for value in tt.values() for v_i in value]
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40