# Can I print an array without type?

**URL:** https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236
**Category:** New to Julia
**Tags:** question
**Created:** [December 17, 2021, 12:27am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236 "2021-12-17T00:27:51Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)
#### Post date: [December 17, 2021, 12:27am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/1 "2021-12-17T00:27:51Z")

</div>

RT.

```julia
julia> A = []
Any[]

julia> push!(A, "a")
1-element Vector{Any}:
 "a"

julia> push!(A, "b")
2-element Vector{Any}:
 "a"
 "b"

julia> println(A)
Any["a", "b"]

```

How can I print A as [“a”, “b”], without Any? Thank you!

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 17, 2021, 12:54am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/2 "2021-12-17T00:54:02Z")

</div>

What for exactly?

One way is

```julia
@show A

```

---

<div class="post-metadata">

### Author: ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)
#### Post date: [December 17, 2021, 12:57am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/3 "2021-12-17T00:57:41Z")

</div>

I’d like to print it as below:  
[“a”, “b”]

not:  
Any[“a”, “b”]

Thanks!

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 17, 2021, 1:10am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/4 "2021-12-17T01:10:52Z")

</div>

```julia
julia> println("["*join(A,",")*"]")
[a,b]

```

I thought that `@show` didn’t show the `Any`. (almost there above, I do not know of any solution which is just what you want)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 17, 2021, 1:20am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/5 "2021-12-17T01:20:43Z")

</div>

> [@Xiao](#):
>
> I’d like to print it as below:  
> [“a”, “b”]

For example, this works:

```julia
julia> println('[', join(repr.(A),", "), ']')
["a", "b"]

```

(Alternatively, using a loop and calling `show` on the elements one by one, you could avoid allocating a bunch of strings just to print them as in the code above, but if the code isn’t performance-critical I wouldn’t worry about it.)

---

<div class="post-metadata">

### Author: ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)
#### Post date: [December 17, 2021, 4:03am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/6 "2021-12-17T04:03:09Z")

</div>

Thanks!

This doesn’t like a straightforward task in Julia as I thought. I need to learn more and understand why.  
Just gave a try in Python, and Python outputs what I want.

```julia
>>> A = []
>>> A.append("a")
>>> A.append("b")
>>> A
['a', 'b']

```

---

<div class="post-metadata">

### Author: ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)
#### Post date: [December 17, 2021, 4:37am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/7 "2021-12-17T04:37:28Z")

</div>

For what purpose do you need/want this?

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [December 17, 2021, 5:51am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/8 "2021-12-17T05:51:01Z")

</div>

With a more precise type specified up front (good practice!), Julia works the same way:

```julia
julia> A = String[]
String[]

julia> push!(A, "a"); push!(A, "b"); println(A)
["a", "b"]

julia> @show A;
A = ["a", "b"]

```

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [December 17, 2021, 6:00am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/9 "2021-12-17T06:00:25Z")

</div>

Wow, that’s new to me. Thank you!

---

<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: [December 17, 2021, 6:13am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/10 "2021-12-17T06:13:14Z")

</div>

> [@Xiao](#):
>
> Python outputs what I want

Python lists don’t _have_ an element type, so there’s nothing to show.

---

<div class="post-metadata">

### Author: ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)
#### Post date: [December 17, 2021, 6:23am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/11 "2021-12-17T06:23:15Z")

</div>

No specific purpose. I just presumed Julia could do it same as or very similarly to other languages. Thanks

---

<div class="post-metadata">

### Author: ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)
#### Post date: [December 17, 2021, 6:31am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/12 "2021-12-17T06:31:27Z")

</div>

Thanks for explaining that.

As noted above with a specific element type you get the output that you want. With ‘Any’, you essentially get the type information printed along.

Maybe a different perspective is the following: To many people seeing this output will be very useful, because arrays of Any type will have poor performance and it’s probably good to highlight such arrays somehow?

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [December 17, 2021, 6:46am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/13 "2021-12-17T06:46:12Z")

</div>

To my knowledge, the type parameters of a container like an Array are fixed on initialization.

```julia-repl
julia> A = [0]
1-element Vector{Int64}:
 0

julia> push!(A, 1.)
2-element Vector{Int64}:
 0
 1

julia> push!(A, 1.5)
ERROR: InexactError: Int64(1.5)

julia> push!(A, "1")
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Int64

```

To your point though, there is definitely some kind of logic for choosing to include the type when printing. I’m not sure if this is documented anywhere.

```julia-repl
julia> println([0f0, 1f0])
Float32[0.0, 1.0]

julia> println([0., 1.])
[0.0, 1.0]

julia> println([0., "1"])
Any[0.0, "1"]

julia> struct Foo end

julia> println([Foo(), Foo()])
[Foo(), Foo()]

```

---

<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: [December 17, 2021, 8:41am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/14 "2021-12-17T08:41:49Z")

</div>

> [@Xiao](#):
>
> No specific purpose. I just presumed Julia could do it same as or very similarly to other languages.

Can Python do this?

```julia
using Unitful
sgj(A) = println('[', join(repr.(A),", "), ']')
A = ["a", "b", 1.0, 1.0u"s", 2.0u"rad"]
push!(A, A[5]/A[4])

# note the last pushed element with proper units:
julia> sgj(A)
["a", "b", 1.0, 1.0 s, 2.0 rad, 2.0 rad s^-1]

```

---

<div class="post-metadata">

### Author: ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)
#### Post date: [December 17, 2021, 9:17am UTC](https://discourse.julialang.org/t/can-i-print-an-array-without-type/73236/15 "2021-12-17T09:17:47Z")

</div>

Thanks. Beautiful code!  
I know almost nothing about Python. I used that example to easily show what I wanted because I believe more Julia experts or learners here already have some experience with Python.
