# How create constant list of integers

**URL:** <https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415>\
**Category:** New to Julia\
**Tags:** arrays\
**Created:** [September 19, 2021, 4:12pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415 "2021-09-19T16:12:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![murrayE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murraye/32/24428_2.png) [@murrayE](https://discourse.julialang.org/u/murrayE)\
**Post date:** [September 19, 2021, 4:12pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/1 "2021-09-19T16:12:24Z")

</div>

Suppose I have a given positive integer `n`. What are _simple_ ways to create a list of length `n` consisting of all zeros exactly, that is, filled with the integer `0`?

For example, if `n` has value 5, then I want to create `[0, 0, 0, 0, 0]` but without explicitly entering five zeros.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [September 19, 2021, 4:16pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/2 "2021-09-19T16:16:16Z")

</div>

`zeros(Int, n)` or `fill(0, n)`

---

<div class="post-metadata">

**Author:** ![murrayE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murraye/32/24428_2.png) [@murrayE](https://discourse.julialang.org/u/murrayE)\
**Post date:** [September 19, 2021, 5:35pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/3 "2021-09-19T17:35:12Z")

</div>

Does Julia have a replicate operator like the one in Python, where, for example, `[0] * 5` produces the list `[0, 0, 0, 0, 0]`?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 19, 2021, 5:40pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/4 "2021-09-19T17:40:01Z")

</div>

```julia
repeat([0], 5)

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 19, 2021, 6:03pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/5 "2021-09-19T18:03:28Z")

</div>

What about:  
`LinRange{Int}(0,0,n) `

---

<div class="post-metadata">

**Author:** ![murrayE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murraye/32/24428_2.png) [@murrayE](https://discourse.julialang.org/u/murrayE)\
**Post date:** [September 19, 2021, 6:51pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/6 "2021-09-19T18:51:42Z")

</div>

OK, `repeat` in Julia works as does python’s `*` replicate operator: it seems to apply generally, too – to strings, matrices, etc. Thank you.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 19, 2021, 6:57pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/7 "2021-09-19T18:57:14Z")

</div>

> [@murrayE](#):
>
> `*` replicate operator

I personally don’t think “\* by a scaler → replication” is very intuitive mathematically

---

<div class="post-metadata">

**Author:** ![murrayE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murraye/32/24428_2.png) [@murrayE](https://discourse.julialang.org/u/murrayE)\
**Post date:** [September 19, 2021, 7:12pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/8 "2021-09-19T19:12:48Z")

</div>

Where is `*` documented? I have not been able to find it in the docs at [julialang.org](http://julialang.org) or through online search elsewhere.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 19, 2021, 7:18pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/9 "2021-09-19T19:18:41Z")

</div>

@murrayE, for strings, `^` is a replicator in Julia:

```julia
julia> "Julia"^5
"JuliaJuliaJuliaJuliaJulia"

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [September 19, 2021, 7:20pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/10 "2021-09-19T19:20:40Z")

</div>

You also have `Tuple` or `ntuple(x->1,6)`

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 19, 2021, 7:22pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/11 "2021-09-19T19:22:05Z")

</div>

hit `?` in REPL and type `*` then hit Enter:

```julia
help?> *
search: *

...
.....

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 19, 2021, 9:23pm UTC](https://discourse.julialang.org/t/how-create-constant-list-of-integers/68415/12 "2021-09-19T21:23:07Z")

</div>

I think you should use `zeros` or `fill` rather than `repeat`. If you have a short list of constant length, known at compile time, you can consider a tuple or StaticArrays.jl.
