liveBook · Manning

Zip

As explained in Julia as a Second Language

zip is a Julia function that combines corresponding elements from multiple iterables into tuples.

zip combines corresponding elements from multiple iterables into tuples. The returned iterable can be collected into an array or passed directly to Dict when separate keys and values must become key-value pairs. section 6.6.1 section 15.4.2

A practical zip workflow is:

Pairing values and building a dictionary

julia> words = ["one", "two"] julia> nums = [1, 2] julia> collect(zip(words, nums)) [("one", 1), ("two", 2)] julia> Dict(zip(words, nums)) Dict("one" => 1, "two" => 2) The same pattern maps an alphabet to a shuffled substitute alphabet for a substitution cipher. section 6.6.1 section 15.4.2

julia> collect(zip(alphabet, substitute))
6-element Vector{Tuple{Char, Char}}:
('A', 'A')
('B', 'C')
('C', 'F')
('D', 'B')
('E', 'D')
('F', 'E')

Inspect the alphabet-to-substitute-alphabet tuples produced by collect(zip(...)) before turning them into a dictionary. section 15.4.2

How Julia as a Second Language covers it

How deep it goes
Explains the iterable result, materialization with collect, and direct consumption by Dict through compact examples.
Examples
Uses word-number pairs and alphabet-to-substitute-alphabet mappings as concrete examples.
What it assumes
Assumes familiarity with arrays and dictionary key-value pairs while demonstrating the missing link between separate arrays and dictionaries.
What you can do after
Apply it via collect or Dict to turn corresponding arrays into tuple collections or lookup mappings.
How it connects
Connects zip with iterables, tuples, arrays, dictionaries, collect, ranges, and substitution ciphers.

Also explained in

Compare how 5 books explain zip →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.