1

As trivial as this questions sounds, an excessive search yield no results. In pandas, you can select single "elements" using df['row1'], and multiple elements using df[['row1', 'row2'].

Since it inhibits a lot of functionality from dictionaries and and numpy, I thought that something along these lines should also be possible for dictionaries: (How) Can I select multiple values from a dictionary?

That is, given

myDict = {'a':1, 'b':2, 'c':3}
myList = ['a', 'c']

is there a simple way to select something as myDict[myList]? Expected outcome is

[1, 3]

I suppose there is some way iterating through the list as foo = [myDict[x] for x in myList], but I'm thinking of straight notation without having to iterate myself.

FooBar
  • 15,724
  • 19
  • 82
  • 171
  • 3
    You cannot select multiple keys from a dictionary, no, there is no dedicated syntax or method. Use a list comprehension, or `operator.itemgetter()`. – Martijn Pieters Sep 05 '14 at 14:02
  • Well, it's nice to have closure. If you post that as an answer, I'll accept it. – FooBar Sep 05 '14 at 14:04
  • @MartijnPieters it actually is not a duplicate in the sense that I saw that question and found it unsatisfactory. It's something else to see Y,Z as solutions proposed than to know that X does not exist. None of the answers there explicitly said that there's no direct way of doing so, which made me continue searching. – FooBar Sep 05 '14 at 14:12
  • But that's because *there is no such syntax*, otherwise that other question would have included that as an answer. – Martijn Pieters Sep 05 '14 at 14:18
  • That's not how SO works though; the answers there answer your question in that *those are the options*. The question there, as worded, isn't that far of from 'what is the syntax for this'. I don't think an answer *there is no special syntax for that* is all that helpful, when I can instead redirect visitors to *this* question to that one that does enumerate all the options you *do* have. – Martijn Pieters Sep 05 '14 at 15:02
  • And thanks for undeleting. – Martijn Pieters Sep 05 '14 at 15:02

0 Answers0