Tuples are a collection of heterogeneous and homogeneous datatypes written as an array, separated by commas. Tuples are basically immutable collections of distinct values. Tuples are different from arrays because arrays only take homogeneous datatypes as values.
NamedTuple is a type of Tuple whose elements can be called in the memory location not just by its index value, but also by a name of the value. Each value in a NamedTuple is represented by a unique value written as a symbol for that value.
Just like Tuples, NamedTuples are also immutable, which means neither the values nor their names once defined, can be changed.
tupl1 = ()
println(isempty(tupl1))
tupl2 = (a = 1 , b = 2 , c = 4 , d = 5 )
println(tupl2)
tupl3 = (a = 1 , b = 2 , c = "Hello Geeks" )
println(tupl3)
|
Output:
Accessing elements from a NamedTuple
Elements from a named tuple can be accessed by using the field index assigned to them or by using the name assigned to the values.
Syntax:
Tuple_Name[Index_value]
or
Tuple_Name[:Value_Name]
Example:
tupl2 = (a = 1 , b = 2 , c = 4 , d = 5 )
println(tupl2[ 2 ])
println(tupl2[ 4 ])
tupl3 = (a = 1 , b = 2 , c = "Hello Geeks" )
println(tupl3[:b])
println(tupl3.c)
|
Output:
Use of getindex() function:
Elements from a NamedTuple can also be accessed with the use of a predefined function in Julia, known as getindex()
. This function accepts Tuple_Name and Key_value/Index_value as parameters and returns the element at that index.
Syntax:
Element = getindex(Tuple_Name, Key_value)
Element = getindex(Tuple_Name, Index_value)
Example:
tupl = (a = 1 , b = 2 , c = "Hello Geeks" )
Value1 = getindex(tupl, :b)
Value2 = getindex(tupl, 3 )
println(Value1)
println(Value2)
|
Output:
Accessing Keys and Values from a NamedTuple:
Keys are the names that are assigned to the values of a NamedTuple. Keys can be accessed in the form of a Tuple with the use of ‘keys’ keyword. Values, on the other hand, can be accessed with the use of ‘values’ keyword. These keywords generate a list of elements in the form of a Tuple.
Syntax:
Keys = keys(Tuple_Name)
Values = values(Tuple_Name)
Example:
tupl = (a = 1 , b = 2 , c = "Hello Geeks" )
Keys = keys(tupl)
Values = values(tupl)
println(Keys)
println(Values)
|
Output:
Operations on NamedTuples
Julia allows to perform various operations on NamedTuples by using some pre-defined keywords. These operations are:
Getting an array of Tuple Values using ‘collect
‘:
Tuple values can also be represented as Arrays with the help of collect
keyword. This keyword can also be used to print key-value pairs with the use of pairs
keyword. This key value pairs is very useful for iterating over tuples.
Syntax:
Array_Name = collect(Tuple_Name)
Array_Name = collect(pairs(Tuple_Name))
Examples:
tupl = (a = 1 , b = 2 , c = 3 , d = 4 )
Array = collect(tupl)
Array2 = collect(pairs(tupl))
println(Array)
println(Array2)
|
Output:
Use of ‘Zip
‘ to create a Tuple:
A NamedTuple can also be created by defining Keys and Values separately as individual Tuples and then combining them together to form a NamedTuple. This can be done by using the keyword ‘zip‘. zip keyword is used along with a semicolon(:) placed inside the tuple.
Syntax:
Keys = (Key1, Key2, Key3, ...)
Values = (Value1, Value2, Value3, ...)
Named_Tuple = (; zip(Keys, Values)
Example:
Key_Tuple = (:a, :b, :c, :d)
Value_Tuple = ( 1 , 2 , 3 , "Hello World" )
Named_Tuple = (; zip (Key_Tuple, Value_Tuple)...)
println( "Keys = " , Key_Tuple)
println( "Values = " , Value_Tuple)
println( "Tuple = " , Named_Tuple)
|
Output:
Use of ‘merge
‘ to combine tuples:
Julia allows merging two or more NamedTuples into one tuple by using a keyword called ‘merge
‘. When merging two tuples if both the tuples have a common key, then the value of the key in the tuple that is created most recently will be used in the final tuple.
Syntax:
Final_tuple = merge(Tuple1, Tuple2, Tuple3, ...)
Example 1:
tupl1 = (a = 1 , b = 2 , c = 3 )
tupl2 = (d = 4 , e = 5 )
tupl3 = merge(tupl1, tupl2)
println(tupl3)
|
Output:
Example 2:
tupl1 = (a = 1 , b = 2 , c = 3 )
tupl2 = (b = "Hello" , c = "Geeks" )
tupl3 = merge(tupl1, tupl2)
println(tupl3)
|
Output:
Last Updated :
27 Feb, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...