Open In App

Getting key value pairs of a dictionary in Julia – pairs() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The pairs() is an inbuilt function in julia which is used to return an iterator over key=>value pairs for the specified collection that maps a set of keys to a set of values. This includes arrays, where the keys are the array indices.

Syntax:
pairs(collection)
or
pairs(IndexStyle(A), A)

Parameters:

  • collection: Specified collection.
  • IndexStyle(A): Specified iterator.
  • A: Specified array.

Returns: It returns an iterator over key=>value pairs for the specified collection that maps a set of keys to a set of values.

Example 1:




# Julia program to illustrate 
# the use of pairs() method
   
# Getting an iterator over key =>value 
# pairs for the specified collection 
# that maps a set of keys to a set of values.
A = ["a" "c"; "b" "d"];
println(pairs(A))


Output:

Example 2:




# Julia program to illustrate 
# the use of pairs() method
   
# Getting an iterator over key =>value 
# pairs for the specified array
A = ["a" "c"; "b" "d"];
for (index, value) in pairs(IndexStyle(A), A)
    println("$index $value")
    end


Output:


Last Updated : 01 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads