0

I would like, from a pair of lists, say:

a = ['str1', 'str2', 'str3', 'str4', 'str5']
b = [var1, var2, var3, var4, var5]

to be able to create multiple dictionaries, with arbitrarily selected pairs of elements from a (as keyword) and b (as value). I have found in a comment on an answer to this question, the following method:

full_set = dict(zip(a,b))
subset = {i:full_set[i] for i in a if i not in ['str2', 'str5']}

which produces subset dictionary:

{'str1': var1, 'str3': var3, 'str4': var4}

This works fine, but I am curious as to whether there is an equally short or shorter method of building subset dictionaries from the two lists, e.g. via specifying the list indices for the elements I want included, without creating a full dictionary containing all elements first.

For context, my vars refer to scikit-learn estimator objects.

Ian F
  • 17
  • 5
  • What are you trying to optimize: time, space or something else? – DYZ Mar 19 '18 at 23:10
  • Minimizing the code being typed :) But the main reason I asked the question was out of curiosity - I couldn't figure out how to build it directly. Anyway, I have tested both methods a few times now using autotime and the np.array method does seems marginally faster, but there isn't a huge difference. Thank you both for your answers, both very helpful - I'm not sure which to choose as correct. – Ian F Mar 19 '18 at 23:56

2 Answers2

3

You can combine both statements into one:

avoid = {'str2', 'str5'} # Sets have better lookup time :)
{k:v for k,v in zip(a,b) if k not in avoid}
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • @AntonvBR So, why is this a problem? – DYZ Mar 19 '18 at 23:08
  • 2
    @AntonvBR "e.g. via specifying the list indices" means "for example, via specifying the list indices." It is not a requirement. – DYZ Mar 19 '18 at 23:12
0

Probably convert this to numpy arrays and use fancy indexing like this:

import numpy as np

a = np.array(['str1', 'str2', 'str3', 'str4', 'str5'])
b = np.array([1,2,3,4,5])

indices = [0, 1, 4]

d = dict(zip(a[indices],b[indices]))

d returns:

{'str1': 1, 'str2': 2, 'str5': 5}
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
  • Selecting this as the answer as it is closer to what I was looking for and is marginally faster. That said, I think I'm going to use DyZ's method as it is probably more easily interpretable - using indices isn't such a smart idea in this context, on reflection. – Ian F Mar 20 '18 at 00:07