48

I want to convert my dictionary to an array, by showing each [String : Int] of the dictionary as a string in the array.

For example:     

var myDict: [String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]

     I'm aware of myDict.keys.array and myDict.values.array, but I want them to show up in an array together. Here's what I mean:     

var myDictConvertedToArray = ["attack 1", "defend 5", "block 12"]
starball
  • 20,030
  • 7
  • 43
  • 238
Nick
  • 2,565
  • 3
  • 13
  • 13

5 Answers5

62

You can use a for loop to iterate through the dictionary key/value pairs to construct your array:

var myDict: [String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]

var arr = [String]()

for (key, value) in myDict {
    arr.append("\(key) \(value)")
}

Note: Dictionaries are unordered, so the order of your array might not be what you expect.


In Swift 2 and later, this also can be done with map:

let arr = myDict.map { "\($0) \($1)" }

This can also be written as:

let arr = myDict.map { "\($0.key) \($0.value)" }

which is clearer if not as short.

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • I have that it doesn't support destructuring pari into (key, value) ($0, $1) so I need to use $0.key instead – Michał Ziobro Mar 15 '18 at 11:26
  • @MichałZiobro, I just retested both answers with Swift 4 in Xcode 9.2 and they both work as stated. What is different about your situation? – vacawama Mar 15 '18 at 12:53
20

The general case for creating an array out of ONLY VALUES of a dictionary in Swift 3 is (I assume it also works in older versions of swift):

let arrayFromDic = Array(dic.values.map{ $0 })

Example:

let dic = ["1":"a", "2":"b","3":"c"]

let ps = Array(dic.values.map{ $0 })

print("\(ps)")

for p in ps {
    print("\(p)")
}
Ali
  • 18,665
  • 21
  • 103
  • 138
7

If you like concise code and prefer a functional approach, you can use the map method executed on the keys collection:

let array = Array(myDict.keys.map { "\($0) \(myDict[$0]!)" })

or, as suggested by @vacawama:

let array = myDict.keys.array.map { "\($0) \(myDict[$0]!)" }

which is functionally equivalent

Antonio
  • 71,651
  • 11
  • 148
  • 165
  • Nice, @Antonio! It could also be written as `let array = myDict.keys.array.map { "\($0) \(myDict[$0]!)" }`. The performance is the same, and 1 whole character shorter. :-) – vacawama Aug 06 '15 at 10:48
7

With Swift 5

var myDict:[String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]

let arrayValues = myDict.values.map({$0})
let arrayKeys = myDict.keys.map({$0})
Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36
2

You will have to go through and construct a new array yourself from the keys and the values.

Have a look at 's swift array documentation:

You can add a new item to the end of an array by calling the array’s append(_:) method:

Try this:

var myDict:[String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]

var dictArray: [String] = []

for (k, v) in myDict {
    dictArray.append("\(k) \(v)")
}

Have a look at What's the cleanest way of applying map() to a dictionary in Swift? if you're using Swift 2.0:

Community
  • 1
  • 1
MrHaze
  • 3,786
  • 3
  • 26
  • 47