julia# Join dict to string

How would in join a dictionary in Julia similar to like what one of the Python ways is:

```
diction = {"DC": 4,
"NH": 1,
"MI": 36,
"MO": 4989,
"FL": 157,
"UT": 13,
"NV": 9,
"CA": 110,
"WV": 6,
"NY": 39,
"ME": 1,
"SC": 26,
"KY": 71}
stri = ' | '.join(map(lambda x: '%2.2s %5.5s' % (str(x[0]), str(x[1])), diction.items()))
```

What I have come up with:

```
diction = Dict(["DC" => 4
"NH" => 1,
"MI" => 36,
"MO" => 4989,
"FL" => 157,
"UT" => 13,
"NV" => 9,
"CA" => 110,
"WV" => 6,
"NY" => 39,
"ME" => 1,
"SC" => 26,
"KY" => 71])
stri = [@sprintf("%2.2s %5.5s | ",st,ct) for st,ct in keys(diction),values(diction)]
```

Yields:

```
ERROR: syntax: invalid iteration specification
```

OUTPUT:

```
DC 4 | NH 1 | MI 36 | MO 4989 | FL 157 | UT 13 | NV 9 | CA 110 | WV 6 | NY 39 | ME 1 | SC 26 | KY 71
```

Solution

In recent Julia versions, you need to pull in the sprintf macros with "using Printf". In addition, when you iterate a dictionary, you need to wrap the key-value pair in parentheses `(st, ct)`

(which Python does not require; this also applies to enumerated for loops). In addition, you should still use a `join`

here, or the line will end with your '|' separator.

This then becomes:

```
using Printf
diction = Dict(["DC" => 4,
"NH" => 1,
"MI" => 36,
"MO" => 4989,
"FL" => 157,
"UT" => 13,
"NV" => 9,
"CA" => 110,
"WV" => 6,
"NY" => 39,
"ME" => 1,
"SC" => 26,
"KY" => 71])
stri = join([@sprintf("%2.2s %5.5s", st, string(ct)) for (st,ct) in diction], " | ")
println(stri)
```

- Julia: is there a function to obtain the version number of a package?
- Get permutation of one array based on the size ordering of the other array
- Making Julia show different plots in different windows
- How to get equally scaled axes with Plots in Julia
- Scatter plot of two rows of a DataFrame in Julia using Plotly
- Adding constraints to jump model from dict
- Non-iterable argument to a function called by Julia `map`?
- Compute row sums and column sums efficiently in Julia
- Julia copying folder into an existing folder
- How to replace a function in Julia?
- How to force Julia to use multiple threads for matrix multiplication?
- Can Revise.jl handle `ERROR: LoadError: invalid redefinition of constant`?
- Define piecewise function with automatic broadcasting in Julia
- julia Handling time difference in dataframe
- Fast tensor-dot on sparse arrays with GPU in any programming language?
- Why do allocations occur during broadcasting assignment to a preallocated array?
- Comparing RK4 to Euler method
- How to put even numbers from matrix in separate vector in Julia?
- Number of iterations performed by a for-loop in Julia
- Can I redefine a function, but still use the old definition within the new definition?
- how do I use analytical form as gradient with ! function?
- Julia equivalent to R `as.numeric()`
- Why does Float16(1.1)-Float16(1)=Float16(0.0996)?
- Julia manual and defining an infix operator
- In Julia, how to convert a unsigned number to a signed number like in C?
- Julia Package DataFrames 1.6.1 doesn't recognize old version DataFrames 1.3.4 file
- Convert Julia DataFrame to an array of bytes for compression
- How to suppress svg output by IJulia in Jupyter notebooks
- how to get the documentation of a julia function in a vscode notebook?
- Passing command-line arguments to a Pluto Notebook