Replacing elements of a collection in Julia - replace() and replace!() Methods
Last Updated :
12 Jul, 2025
The
replace()
is an inbuilt function in
julia which is used to return a copy of the specified array with different types of replacement. These all operations are illustrated with below examples.
Syntax:
replace(A, old_new::Pair...; count::Integer)
or
replace(new::Function, A; count::Integer)
Parameters:
- A: Specified array.
- old_new::Pair: Specified values to get updated.
- count::Integer: This optional count value is used to replace at most count occurrences in total.
- new::Function: Specified set of instruction.
Returns: It returns a copy of the specified array with different types of replacement.
Example 1: This example returns a copy of the specified collection. In this collection old value get replaced with new value and if count is specified, then replace at most count occurrences in total.
Python
# Julia program to illustrate
# the use of replace() method
# Getting a copy of the specified collection.
# In this collection old value get replaced
# with new value and if count is specified,
# then replace at most count occurrences in total.
println(replace([1, 3, 5, 4], 3 =>2, 5 =>3, count = 1))
println(replace([1, 3, 5, 4], 3 =>2, 5 =>3))
println(replace([1, false], false =>0))
println(replace([1, true], true =>1))
Output:
Example 2: This example returns a copy of a specified array where each value x in the array is replaced by new(x).
Python
# Julia program to illustrate
# the use of replace() method
# Getting a copy of a specified array
# where each value x in the array is
# replaced by new(x).
println(replace(x -> isodd(x) ? 2x : x, [1, 2, 3, 4]))
println(replace(x -> iseven(x) ? 2x : x, [1, 2, 3, 4]))
Output:
replace!()
The
replace!()
is an inbuilt function in
julia which is used to return the replaced specified array with different types of replacement. These all operations are illustrated with below examples.
Syntax:
replace!(A, old_new::Pair...; count::Integer)
or
replace!(new::Function, A; count::Integer)
Parameters:
- A: Specified array.
- old_new::Pair: Specified values to get updated.
- count::Integer: This optional count value is used to replace at most count occurrences in total.
- new::Function: Specified set of instruction.
Returns: It returns the replaced array with different types of replacement method.
Example 1: This example returns the replaced collection. In this collection old value get replaced with new value and if count is specified, then replace at most count occurrences in total.
Python
# Julia program to illustrate
# the use of replace !() method
# Getting the replaced collection
# In this collection old value get replaced
# with new value and if count is specified,
# then replace at most count occurrences in total.
println(replace !([1, 3, 5, 4], 3 =>2, 5 =>3, count = 1))
println(replace !([1, 3, 5, 4], 3 =>2, 5 =>3))
println(replace !([1, false], false =>0))
println(replace !([1, true], true =>1))
Output:
Example 2: This example returns the replaced specified array where each value x in the array is replaced by new(x).
Python
# Julia program to illustrate
# the use of replace !() method
# Getting the replaced specified array
# where each value x in the array is
# replaced by new(x).
println(replace !(x -> isodd(x) ? 2x : x, [1, 2, 3, 4]))
println(replace !(x -> iseven(x) ? 2x : x, [1, 2, 3, 4]))
Output:
Similar Reads
Creating array with repeated elements in Julia - repeat() Method The repeat() is an inbuilt function in julia which is used to construct an array by repeating the specified array elements with the specified number of times. Syntax: repeat(A::AbstractArray, counts::Integer...) or repeat(A::AbstractArray; inner, outer) or repeat(s::AbstractString, r::Integer) or re
2 min read
Replace a substring with another string in Julia - replace() Method The replace() is an inbuilt function in julia that is used to replace a word or character with the specified string or character. Syntax: replace(s::AbstractString, pattern=>Word; count::Integer) Parameters:  s::AbstractString: Specified string.pattern=>Word: Pattern is searched from the gi
1 min read
Apply operations on a collection in Julia - map() and map!() Methods The map() is an inbuilt function in julia which is used to transform the specified collection by using specified operation to each element. Syntax: map(f, c...) Parameters: f: Specified operation. c: Specified collection. Returns: It returns the transformed elements. Example: Python # Julia program
2 min read
Accessing each element of a collection in Julia - foreach() Method The foreach() is an inbuilt function in julia which is used to call function f on each element of the specified iterable c. Syntax: foreach(f, c...) Parameters: f: Specified set of instructions. c: Specified iterable. Returns: It returns the result of the operation of function f on each element of t
1 min read
Get repetition of a string in Julia - repeat() Method The repeat() is an inbuilt function in julia which is used to return a string which is the repetition of specified string with specified number of times. Syntax: repeat(String::AbstractString, r::Integer) Parameters: String: Specified string or character r::Integer: Specified number of times Returns
2 min read
Reverse array elements in Julia - reverse(), reverse!() and reverseind() Methods The reverse() is an inbuilt function in julia which is used to reverse the specified vector v from start to stop. Syntax: reverse(v, start, stop) or reverse(A; dims::Integer) Parameters: v: Specified vector. start: Specified starting value. stop: Specified stopping value. A: Specified array. dims::I
2 min read