# Equivalent of list() (R) in Julia

**URL:** <https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860>\
**Category:** Data\
**Created:** [March 24, 2017, 1:20pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860 "2017-03-24T13:20:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Droxler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan_droxler/32/9451_2.png) [@Jonathan\_Droxler](https://discourse.julialang.org/u/Jonathan_Droxler)\
**Post date:** [March 24, 2017, 1:20pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/1 "2017-03-24T13:20:33Z")

</div>

Dear all,

Quite new in Julia (I’m already a big fan), I was wondering if there exist any data structure in Julia like “list()” in R. I search in the documentation, on google. Maybe I didn’t search well but I didn’t find anything.

I’m sorry if this a recurrent post.

Thank you very much and have a nice day

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 24, 2017, 1:26pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/2 "2017-03-24T13:26:58Z")

</div>

You mean a list with objects of different types? This is the default in Julia:

```julia
a = ["Julia is", 100, '%', :cool]

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 24, 2017, 1:27pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/3 "2017-03-24T13:27:56Z")

</div>

You can use a vector as a list – you can do all of the standard dequeue operations on it, including `push!`, `pop!`, `shift!` and `unshift!`.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Droxler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan_droxler/32/9451_2.png) [@Jonathan\_Droxler](https://discourse.julialang.org/u/Jonathan_Droxler)\
**Post date:** [March 24, 2017, 1:39pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/4 "2017-03-24T13:39:10Z")

</div>

Ok thank you for that.

But I also think to have something like

> list\_a  
> $A  
> $A$a  
> some\_data\_a  
> $A$b  
> some\_other\_data\_a  
> $B  
> $B$a  
> some\_data\_b  
> $B$b  
> some\_other\_data\_b

I can do recursively vector with data, but is there a way after to access my data with name, like

> list\_a$B$b  
> some\_other\_data\_b

Thank you very much

---

<div class="post-metadata">

**Author:** ![Jonathan\_Droxler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan_droxler/32/9451_2.png) [@Jonathan\_Droxler](https://discourse.julialang.org/u/Jonathan_Droxler)\
**Post date:** [March 24, 2017, 1:40pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/5 "2017-03-24T13:40:05Z")

</div>

And I agree with

a = [“Julia is”, 100, ‘%’, :cool]

🙂

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [March 24, 2017, 1:55pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/6 "2017-03-24T13:55:48Z")

</div>

There’s no exact equivalent, but a `Dict` (or an `OrderedDict` from package DataStructures if order matters) is probably the closest structure.

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [March 24, 2017, 2:44pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/7 "2017-03-24T14:44:15Z")

</div>

You could use a `Vector` of types:

```nohighlight
type myt
    a::Matrix{Float64}
    b::String
end

list = [myt(randn(10,10),"some_data"),
    myt(rand(5,5),"other_data") ]

list[1].b
list[2].b

```

So elements of the type named, and elements of the `Vector` are indexed by an integer. Note the keyword changes to types in [Julia 0.6](https://github.com/JuliaLang/julia/blob/master/NEWS.md).

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 24, 2017, 3:06pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/8 "2017-03-24T15:06:13Z")

</div>

Yes an OrderedDict should be the same as an R list, except that you cannot index it with numbers.

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [March 24, 2017, 3:59pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/9 "2017-03-24T15:59:04Z")

</div>

@Jonathan_Droxler: The list object in R is very similar to `Dict()` in Julia. I have been fairly using `Dict()` in Julia to transfer my R list objects to Julia.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Droxler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan_droxler/32/9451_2.png) [@Jonathan\_Droxler](https://discourse.julialang.org/u/Jonathan_Droxler)\
**Post date:** [March 24, 2017, 4:41pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/10 "2017-03-24T16:41:01Z")

</div>

Thank you very much.

Dict() is indeed very usefull.

Thank you and have pleasure with Julia 😉

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [March 24, 2017, 5:44pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/11 "2017-03-24T17:44:35Z")

</div>

You may want to look at the `RCall` package as one of its purposes is to map `R` objects to `Julia` types and vice-versa. It even allows you to enter `R` code in the `Julia` REPL for evaluation in an inferior R process. (The word “inferior” is not a value judgement here, it means that the Julia process has started and controls and R process).

In this transcript, one switches to the `R>` prompt by typing a `$` at the `julia>` prompt. The backspace switches back to `julia>`.

```julia
julia> using RCall
INFO: Using R installation at /usr/lib/R

R> mylist <- list(A = list(a=1L, b="foo"), B=list(aa=2.14, b="baz"), C = "boz")

R> str(mylist)
List of 3
 $ A:List of 2
  ..$ a: int 1
  ..$ b: chr "foo"
 $ B:List of 2
  ..$ aa: num 2.14
  ..$ b : chr "baz"
 $ C: chr "boz"

julia> rcopy(R"mylist")
Dict{Symbol,Any} with 3 entries:
  :A => Dict{Symbol,Any}(Pair{Symbol,Any}(:a, 1),Pair{Symbol,Any}(:b, "foo"))
  :B => Dict{Symbol,Any}(Pair{Symbol,Any}(:b, "baz"),Pair{Symbol,Any}(:aa, 2.14))
  :C => "boz"

```

---

<div class="post-metadata">

**Author:** ![randy3k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randy3k/32/878_2.png) [@randy3k](https://discourse.julialang.org/u/randy3k)\
**Post date:** [March 25, 2017, 4:39am UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/12 "2017-03-25T04:39:03Z")

</div>

While named R list is like Dict or OrderedDict.  
Unnamed R list is not exactly Dict as order of the members matters. It is more like Vector{Any}.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 25, 2017, 6:25am UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/13 "2017-03-25T06:25:50Z")

</div>

Or you can mix named and unnamed:

```nohighlight
list(1, a=2, b=3) # R code

```

There is **no** equivalent of R’s `list()` in Julia, but since like most R constructs it is a DWIM monster that would lead to horrible code in Julia (total lack of type stability, etc), it may be for the best.

Generally it is better to ask about solving a specific problem (“I want to do …”) instead of asking about the equivalent of something in another language, since besides trivial cases, there isn’t one, so respondents try to guess what the OP wanted.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 25, 2017, 6:41am UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/14 "2017-03-25T06:41:47Z")

</div>

Just to give a full answer - R and Julia divides their containers along different criteria, so it is hard to talk about strict equivalence. Here is a short review:

R divides the main container type by heterogeneity: `list` takes objects of any type and allows for different types, `vector` takes strings and primitives and only objects of the same type. In addition to this definition there are some behavioral differences: `list` support indexing by name _with the $, `vector` also supports named indexing but in a different way_ (the bit in italics has been edited based on replies below). And indexing into a `list` always returns a `list` - returning a `list` element requires double indexing (`[[]]`), whereas indexing into a vector with a scalar returns an element (that is because scalars in R are by definition just length-1 vectors). Finally, `list` doubles as user types - a user type (S3) is just a `list` with certain named fields.

In julia, these divisions are orthogonal and independent. All types are treated equal, so all containers can take both user types and primitives (primitives can also be user types). Whether elements are the same or different types is signified by the Type Parameter, i.e. is it a `Vector{Int}` or a `Vector{Any}`, for any container type. Container types are divided by behavior: The division between Dicts and Vectors is whether you access by name or by a numeric index (and frankly in R you rarely use both on the same object). Indexing a container with a Vector also gives a Vector, whereas indexing with a scalar returns an element. Which doesn’t cause problems in applications, because indexing with a length-1 Vector will not be interpreted as a scalar (which is the issue double indexing in R seeks to avoid). Finally user types have their own, powerful, system.

A common use case for lists in R is to return multiple values from a function. In Julia, you’d either build a user type (if you want to keep them together) or return the values in a Tuple (for ad hoc use).

So that is why you cannot say which julia type is equivalent to a list - I personally think you will prefer julia’s system once you get acquainted with it.

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [March 25, 2017, 2:52pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/15 "2017-03-25T14:52:56Z")

</div>

> [@mkborregaard](#):
>
> list support indexing by name, while vector doesn’t

```R
> A = c("a"=1,"b"=2)
> str(A)
 Named num [1:2] 1 2
 - attr(*, "names")= chr [1:2] "a" "b"
> A["a"]
a
1
> A["b"]
b
2

```

🙂

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 25, 2017, 4:12pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/16 "2017-03-25T16:12:15Z")

</div>

Thats funny - I use named vectors in R all the time and have never used the names for indexing. I wonder if this was added after I learnt R… I seem to not be the only one who has loosely associated R `list`s with julia `Dict`s because of the named indexing 🙂

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [March 27, 2017, 6:40pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/17 "2017-03-27T18:40:05Z")

</div>

Indexing by name has been in R before there was an R. R is based on an earlier language S developed at Bell Labs and indexing by name was part of S.

All user-visible R objects can have “attibutes”, one of which can be names.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 27, 2017, 6:55pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/18 "2017-03-27T18:55:05Z")

</div>

Just an oversight then 🙂 Thanks for pointing this out. I wonder how often vector indexing by name is used in the wild?

Also @Jonathan_Droxler, one other aspect of Dicts that it is good for people coming from R to be aware of is that they don’t need to be indexed with a ‘name’, i.e. indexes can be any type, not just a string or symbol. I find that useful surprisingly often.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 28, 2017, 5:55am UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/19 "2017-03-28T05:55:47Z")

</div>

Oh and maybe finally worth mentioning that named arrays are possible in Julia with the NamedArrays.jl package.

---

<div class="post-metadata">

**Author:** ![gwangjinkim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gwangjinkim/32/12544_2.png) [@gwangjinkim](https://discourse.julialang.org/u/gwangjinkim)\
**Post date:** [January 30, 2020, 8:54pm UTC](https://discourse.julialang.org/t/equivalent-of-list-r-in-julia/2860/20 "2020-01-30T20:54:31Z")

</div>

I think this is maybe still the best answer. I was thinking, there must be sth like ‘NamedArrays.jl’ - however, one thing Julia does not emulate are attributes in R which any object can have (it is very Common Lisp (CL) style - where there are property lists assignable to any object - even single symbols - some people might not like it but there is meta-info storable - so actually quite cool for metaprogramming). Probably to imitate fully R’s lists, one should create custom structs in Julia …
