-1

Can I shorten this code into 1-2 lines?

I've tried making it into a list comprehension but I'm not sure how it would work if one of the for loops uses a dictionary key.

for a in dict.keys():  
    for q in list:  
        if a in q:  
            dict[a].append(q)  

This code works well to find if the items in the list are one of the dictionary's keys and stores the item as a value under that dictionary key if it is, but I'm hoping to figure out if I can simplify it more.

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
  • 2
    Can you show an example of how this is supposed to be used? There are ways to shorten it but the best one needs more detail – Ry- Jan 13 '19 at 19:00
  • 4
    what datatype is `q`? is `q` going to be an exact match with a dictionary key? Could you provide a sample dict and list? – Paritosh Singh Jan 13 '19 at 19:00
  • I second that, I went for dictionary comprehension and then spotted you're actually modifying the input dict... so, please, try a bit more detail of what is it you are after. Also note, do not use `dict` or `list` as variable names. Doing so you reassign names of built-in types constructors to a different object. – Ondrej K. Jan 13 '19 at 19:13

2 Answers2

1

Initially I wanted to suggest this:

{k: v for v in mylist for k in mydict if k in v}

But then I've noticed you're actually modifying your inputs along the way and it's not what is going on. This should actually do the same (I've taken an assumption or two about the inputs):

for key in mydict.keys():
    mydict[key].extend((i for i in mylist if key in i))

But a piece of unsolicited advice. The code snippet really does not need shortening. Rather it could do with a bit more verbosity and more descriptive variable names to guide a reader through and helping to understand what is going on.

In any case, please, do not use variable names like dict or list as those are already used for built-in type constructor and it really is asking for trouble to reassign them to a different object (under most circumstances and even if carefully considered and intended, I'd still advise against it).

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
-1

If you want, you can turn it into a list comprehension like so: [my_dict[a].append(q) for a in my_dict for q in my_list if a in q], but, as you can see in the comments, this is not recommended and may actually be harder to read & understand.

What the loops use to iterate over is not important. What I took so time time to understand is that the for get written in the order they are in the nested loop structure, not inverted as the expression to evaluate.

user24343
  • 892
  • 10
  • 19
  • 1
    No, don't use `list.append` in a list comprehension: [Is it Pythonic to use list comprehensions for just side effects?](https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects) – jpp Jan 13 '19 at 19:01
  • 2
    That's not the best way to turn this into a comprehension because this produces an unneeded list of `None`s in the end. – Norrius Jan 13 '19 at 19:01
  • 2
    this is essentially the same code without any simplification, and using list comprehension for a side effect, which is not recommended. – Paritosh Singh Jan 13 '19 at 19:02