How can I combine two arrays into a dictionary?

I have 2 arrays:

var identic = [String]() var linef = [String]() 

I added them with the data. Now for ease of use, my goal is to combine them all into a dictionary with the following structure

 FullStack = ["identic 1st value":"linef first value", "identic 2nd value":"linef 2nd value"] 

I was browsing the web and could not find a viable solution.

Any ideas are welcome.

Thanks!

+12
source share
6 answers

Use enumerated() :

 var arrayOne: [String] = [] var arrayTwo: [String] = [] var dictionary: [String: String] = [:] for (index, element) in arrayOne.enumerated() { dictionary[element] = arrayTwo[index] } 

Pro approach will use the extension:

 extension Dictionary { public init(keys: [Key], values: [Value]) { precondition(keys.count == values.count) self.init() for (index, key) in keys.enumerate() { self[key] = values[index] } } } 

Edit: enumerate()enumerated() (Swift 3 → Swift 4)

+26
source

Starting with Xcode 9.0, you can simply do:

 var identic = [String]() var linef = [String]() // Add data... let fullStack = Dictionary(uniqueKeysWithValues: zip(identic, linef)) 

If your keys are not guaranteed to be unique, use instead:

 let fullStack = Dictionary(zip(identic, linef), uniquingKeysWith: { (first, _) in first }) 

or

 let fullStack = Dictionary(zip(identic, linef), uniquingKeysWith: { (_, last) in last }) 

Documentation:

+22
source

A slightly different method that does not require arrays to be the same length, because the zip function will safely handle this.

 extension Dictionary { init(keys: [Key], values: [Value]) { self.init() for (key, value) in zip(keys, values) { self[key] = value } } } 
+20
source

If you want to be more secure and make sure you select the counter of the smaller array each time (so that you cannot crash if the second array is smaller than the first), do the following:

 var identic = ["A", "B", "C", "D"] var linef = ["1", "2", "3"] var Fullstack = [String: String]() for i in 0..<min(linef.count, identic.count) { Fullstack[identic[i]] = linef[i] } print(Fullstack) // "[A: 1, B: 2, C: 3]" 
+6
source

This is a common solution.

 func dictionaryFromKeysAndValues<K : Hashable, V>(keys:[K], values:[V]) -> Dictionary<K, V> { assert((count(keys) == count(values)), "number of elements odd") var result = Dictionary<K, V>() for i in 0..<count(keys) { result[keys[i]] = values[i] } return result } var identic = ["identic 1st value", "identic 2nd value", "identic 3rd value"] var linef = ["linef 1st value", "linef 2nd value", "linef 3rd value"] let mergedDictionary = dictionaryFromKeysAndValues(identic, linef) 
+2
source

Here is an extension that combines some of the previous answers and accepts all sequences, not just arrays.

 public extension Dictionary { init<K: Sequence, V: Sequence>(keys: K, values: V) where K.Element == Key, V.Element == Value, K.Element: Hashable { self.init() for (key, value) in zip(keys, values) { self[key] = value } } } 

This extension does not require the sequences to be the same length. If you want this, here is an extension with statements.

 public extension Dictionary { init<K: Sequence, V: Sequence>(keys: K, values: V) where K.Element == Key, V.Element == Value, K.Element: Hashable { self.init() var keyIterator = keys.makeIterator() for value in values { let key = keyIterator.next() assert(key != nil, "The value sequence was longer.") self[key!] = value } assert(keyIterator.next() == nil, "The key sequence was longer.") } } 
0
source

All Articles