Dictionaries vs NamedTuples

namedtuple vs dict performance
python namedtuple
namedtuple vs tuple
python iterate namedtuple
namedtuple constructor
python namedtuple vs class
python tuple vs dictionary performance
namedtuple defaults

Aside that Dictionaries are mutable and NamedTuple not, that NamedTuple can be retrieved by position and a bit of different notation, are there other significant differences between Dictionaries and NamedTuples in Julia ? When to use one or the other ?

They seem pretty similar:

# Definition
d  = Dict("k1"=>"v1", "k2"=>"v2")
nt = (k1="v1", k2="v2")

# Selection by specific key
d["k1"]
nt.k1

# Keys
keys(d)
keys(nt)

# Values
values(d)
values(nt)

# Selection by position
d[1] # error
nt[1]

Think of NamedTuple as an anonymous struct in Julia not a Dict. In particular storing heterogeneous types in NamedTuple is type stable.

Note that this is also a major difference in thinking between Python and Julia. In Julia if you want your code to be fast you usually care about type inference.

Dictionaries vs NamedTuples, Python namedtuple is just as memory-efficient as a regular tuple, because it does not have per-instance dictionaries. This is also why it is faster than a dictionary. A significant difference in Julia is that a NamedTuple is its own type, and therefore the compiler can specialize on that particular named tuple signature, while the Dictionary approach has to look up the value from the key.

A significant difference in Julia is that a NamedTuple is its own type, and therefore the compiler can specialize on that particular named tuple signature, while the Dictionary approach has to look up the value from the key. In addition, each value of a NamedTuple can be a different type itself, allowing for further optimization and type stability beyond what can be achieved in a dictionary. If we change it a bit more so that the type of the dictionary is Heterogeneous so that it is of type Dict{Symbol,Any}, you can see how it can still be type stable.

d=Dict(:k1=>"v1",:k2=>2.0)
nt=(k1="v1",k2=2.0)

foo(x) = x[:k2]

Now if a function uses this dictionary or named tuple directly, we can see for the dictionary type, the result is not type stable, as the value in the Dictionary can only be guaranteed to by the type of Any.

@code_warntype foo(d)

Body::Any
4 1 ─ %1 = invoke Base.ht_keyindex(_2::Dict{Symbol,Any}, :k2::Symbol)::Int64                                                                                                                                               │╻  getindex
  │   %2 = (Base.slt_int)(%1, 0)::Bool                                                                                                                                                                                     ││╻  <
  └──      goto #3 if not %2                                                                                                                                                                                               ││
  2 ─ %4 = %new(Base.KeyError, :k2)::KeyError                                                                                                                                                                              ││╻  Type
  │        (Base.throw)(%4)                                                                                                                                                                                                ││
  └──      $(Expr(:unreachable))                                                                                                                                                                                           ││
  3 ─ %7 = (Base.getfield)(x, :vals)::Array{Any,1}                                                                                                                                                                         ││╻  getproperty
  │   %8 = (Base.arrayref)(false, %7, %1)::Any                                                                                                                                                                             ││╻  getindex
  └──      goto #5                                                                                                                                                                                                         ││
  4 ─      $(Expr(:unreachable))                                                                                                                                                                                           ││
  5 ┄      return %8

On the other hand, a NamedTuple can be type stable. Because the field was known by the function, the type is also known to be a Float64.

@code_warntype foo(nt)

Body::Float64
4 1 ─ %1 = (Base.getfield)(x, :k2)::Float64                                                                                                                                                                                     │╻ getindex
  └──      return %1

Python Namedtuple, I would use a namedtuple : from collections import namedtuple Filesize = namedtuple('Filesize', 'filename size') file  With namedtuples you don’t have to use integer indexes for accessing members of a tuple. You can think of namedtuples like dictionaries but unlike dictionaries they are immutable. from collections import namedtuple Animal = namedtuple ('Animal', 'name age type') perry = Animal (name="perry", age=31,

When to use a dictionary vs tuple in Python, namedtuples are tuples, meaning they are stored efficiently, and are constant (​thus can also be used as dictionary keys). Unlike regular tuples  This is a documented method of namedtuples, i.e. unlike the usual convention in python the leading underscore on the method name isn't there to discourage use. Along with the other methods added to namedtuples, _make , _replace , _source , _fields , it has the underscore only to try and prevent conflicts with possible field names.

Are namedtuples that popular? They always felt awkward to me. If , Moreover, as namedtuple instances do not have per-instance dictionaries, they are lightweight and require no more memory than regular  Namedtuple in Python Python supports a type of container like dictionaries called “ namedtuples() ” present in module, “ collections “. Like dictionaries they contain keys that are hashed to a particular value. But on contrary, it supports both access from key value and iteration, the functionality that dictionaries lack.

Why should you use namedtuple instead of a tuple? – Python Tips, Namedtuples can be a great alternative to defining a class manually and they have some other It returns the contents of a namedtuple as a dictionary:. Defining a named tuple from dictionary is a one-liner, nt= namedTupleConstructor(**d). The first line is the definition of a new class, which is supposed to be re-used. – FLab May 11 '17 at 16:48 add a comment |

Writing Clean Python With Namedtuples – dbader.org, It is often much faster than creating a new dictionary and running multiple Named tuples assign meaning to each position in a tuple and allow for more  1. Unlike a regular tuple, python namedtuple can also access values using names of the fields. >>> red.red. 255 2. Python namedtuple is just as memory-efficient as a regular tuple, because it does not have per-instance dictionaries. This is also why it is faster than a dictionary. 7. Conclusion. Today, we learned about Python namedtuple.

Comments
  • So, this "answer" is definitely wrong. I keep it alive, as it is a good reminder that there are differences between Python and Julia ;-)