push!
Continuing on previous post, let’s start with creating an Dictionary
object.
eng2kor=Dict(
"one"=>"일",
"two"=>"이",
"three"=>"삼",
"four"=>"사"
)
Output:
Dict{String, String} with 4 entries:
"two" => "이"
"four" => "사"
"one" => "일"
"three" => "삼"
push!
allows us to add one additional key-value pair if the added key is not present in the existing Dictionary, or if it is present, replace existing key with different value.
# replace with existing key with different value
push!(eng2kor, "four"=>"😃")
Output:
Dict{String, String} with 4 entries:
"two" => "이"
"four" => "😃"
"one" => "일"
"three" => "삼"
# add new key-value pair
push!(eng2kor, "five"=>"다섯")
Output:
Dict{String, String} with 5 entries:
"two" => "이"
"four" => "😃"
"one" => "일"
"three" => "삼"
"five" => "다섯"
Contrary to unordered Dictionary
, Array
is an ordered mutable list, which implies that when push!
is applied to an Array
, it will add elements to the end of the array.
push!
applied to an Array
would
a=[1,2,3,4,5]
push!(a, 6,7)
Output:
7-element Vector{Int64}:
1
2
3
4
5
6
7
pop!
pop!
will remove the specified key with its associated value, when applied to Dictionary
object.
pop!(eng2kor, "one")
Output:
Dict{String, String} with 4 entries:
"two" => "이"
"four" => "😃"
"three" => "삼"
"five" => "다섯"
If applied to an Array
, it will remove the last element of the array.
pop!(a)
Output:
6-element Vector{Int64}:
1
2
3
4
5
6
Reference
A Gentle Introduction to Julia, https://www.youtube.com/watch?v=4igzy3bGVkQ