Range For Keys and Values Of Dictionary

I came across a code

let myFruitBasket = ["apple":"red", "banana": "yellow", "budbeeri": "dark voilet", "chikoo": "brown"]

Can we have range for keys and values of dictionary, it will be convenient

for keys

print(myFruitBasket.keys[1...3])

// banana, budbeeri, chikoo

same for values

print(myFruitsBasket.values[1...3])

// yellow, voilet, brown

Answered by DTS Engineer in 848555022

It would help if you posted more details about what you plan to do with these ranges. However, what you’re asking for is supported, because the values returned by both keys and values conforms to Collection. So, for example, you can do this:

let myFruitBasket = ["apple":"red", "banana": "yellow", "budbeeri": "dark voilet", "chikoo": "brown"]
print(myFruitBasket.keys.dropFirst())

This doesn’t print a nice value but you can solve that by converting to an array:

print(Array(myFruitBasket.keys.dropFirst()))
// ["chikoo", "apple", "banana"]

IMPORTANT The keys and values collection doesn’t guarantee the order, which is why this doesn’t print the value you’re expecting.

And that means that dropping the first element only makes sense in some cases. And hence my earlier request for more details about what you’re planning to do with these ranges.

ps You might want to ask questions like this over in Swift Forums > Using Swift. I’m happy to answer them here, but there’s a wider audience of Swift aficionados over there.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

It would help if you posted more details about what you plan to do with these ranges. However, what you’re asking for is supported, because the values returned by both keys and values conforms to Collection. So, for example, you can do this:

let myFruitBasket = ["apple":"red", "banana": "yellow", "budbeeri": "dark voilet", "chikoo": "brown"]
print(myFruitBasket.keys.dropFirst())

This doesn’t print a nice value but you can solve that by converting to an array:

print(Array(myFruitBasket.keys.dropFirst()))
// ["chikoo", "apple", "banana"]

IMPORTANT The keys and values collection doesn’t guarantee the order, which is why this doesn’t print the value you’re expecting.

And that means that dropping the first element only makes sense in some cases. And hence my earlier request for more details about what you’re planning to do with these ranges.

ps You might want to ask questions like this over in Swift Forums > Using Swift. I’m happy to answer them here, but there’s a wider audience of Swift aficionados over there.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

You may build something custom.

Just for illustration:

typealias FruitDict = [String: (String, Int)]

let myFruitBasket : FruitDict = ["apple":("red", 1), "banana": ("yellow", 2), "budbeeri": ("dark violet", 3), "chikoo": ("brown", 4)]

func subBasket(_ basket: FruitDict, range: ClosedRange<Int>) -> [String: String] {
    let filtered = basket.filter {
        $0.value.1 >= range.lowerBound && $0.value.1 <= range.upperBound
          }
    let sub = filtered.mapValues{ value in value.0 }
    return sub
}

let sub: [String: String] = subBasket(myFruitBasket, range: 1...3)

print(sub)
print("keys: ", sub.keys)
print("values", sub.values)

You get:

["banana": "yellow", "apple": "red", "budbeeri": "dark violet"]
keys:  ["banana", "apple", "budbeeri"]
values ["yellow", "red", "dark violet"]

Of course, that's not ordered (order does not mean anything with dictionaries), as Quinn explained. But if you plan to use the result as a dictionary, that doesn't matter.

To get ordered, you have to convert to array. Or you can compute:

let sortedKeys = sub.keys.sorted()
print("sorted keys: ", sortedKeys)
for key in sortedKeys {
    print("sorted values", sub[key] ?? "")
}

And get

sorted keys:  ["apple", "banana", "budbeeri"]
values red
values yellow
values dark violet

Actually I was trying this, Thanks for the posts! Cheers..!

let number = Array(myFruitBasket.keys)
print(number.sorted())
print(number[1...3])

and the answer is:

["apple", "banana", "budbeeri", "chikoo"]
["banana", "budbeeri", "chikoo"]

@SaurabhSaini thanks for the feedback.

But your OP asked for selecting range for keys and values. I do not see how your solution achieves it.

Range For Keys and Values Of Dictionary
 
 
Q