List comp help

J

Joseph L. Casale

I have a dict of lists. I need to create a list of 2 tuples, where each tuple is a key from
the dict with one of the keys list items.

my_dict = {
'key_a': ['val_a', 'val_b'],
'key_b': ['val_c'],
'key_c': []
}
[(k, x) for k, v in my_dict.items() for x in v]

This works, but I need to test for an empty v like the last key, and createone tuple ('key_c', None).
Anyone know the trick to reorganize this to accept the test for an empty v and add the else?

Thanks!
jlc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Dict comp help 0
Range / empty list issues?? 1
Dict comprehension help 0
Help for my project in the last minute 0
Mutability issue 1
enhancing 'list' 10
Optimizing list processing 17
tallying occurrences in list 9

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top