title: How to extract a subset of a swift 3 Dictionary
body: <p>I've looked through the <a href="https://developer.apple.com/reference/swift/dictionary" rel="nofollow noreferrer">methods here</a> but I can't quite find what I'm looking for. I'm new-ish to Swift. I would like to extract a subset from a Dictionary based on a Set of key values, preferably without a loop.</p>
  
  <p>For example, if my key Set is of type <code>Set&lt;String&gt;</code>  and I have a Dictionary of type <code>Dictionary&lt;String, CustomObject&gt;</code>, I would like to create a new Dictionary of type <code>Dictionary&lt;String, CustomObject&gt;</code> that contains only the key-value pairs associated with the keys in the Set of Strings. </p>
  
  <p>I can see that I could do this with <code>for</code> loop, by initializing a new <code>Dictionary&lt;String, CustomObj&gt;()</code>, checking if the original Dictionary contains a value at each String in the set, and adding key-value pairs to the new Dictionary. I am wondering if there is a more efficient/elegant way to do this however.</p>
  
  <p>I'd be open to finding the subset with an Array of Strings instead of a Set if there is a better way to do it with an Array of keys.</p>
  
  <p>Many thanks!</p>