From baf34f6b572c4549721cfe753c9636cdaadafa3f Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Wed, 14 Jun 2023 06:58:07 +0100 Subject: [PATCH] python: zip function --- .../Python/Syntax/Zip_function_in_Python.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Programming_Languages/Python/Syntax/Zip_function_in_Python.md diff --git a/Programming_Languages/Python/Syntax/Zip_function_in_Python.md b/Programming_Languages/Python/Syntax/Zip_function_in_Python.md new file mode 100644 index 0000000..33c17bd --- /dev/null +++ b/Programming_Languages/Python/Syntax/Zip_function_in_Python.md @@ -0,0 +1,50 @@ +--- +categories: + - Programming Languages +tags: [python, data-structures] +--- + +# `zip` + +The `zip` function returns a zip object when you pass in two iterator data types (typically two lists). This is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together, and so on. + +## Example + +```py +a = ("John", "Charles", "Mike") +b = ("Jenny", "Christy", "Monica", "Vicky") + +x = zip(a,b) + +print(x) +# + +# To display the resulting tuple in a readable format: + +print(tuple(x)) + +(('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica')) +``` + +As indicated above, if the input iterators are of equal lengths, Python will ignore the value in iterator B that doesn't have an equivalent index in iterator A. + +## Real-life example + +```py +prompts = [ + "Enter title", + "Enter author", + "Enter publication date", + "Enter year you read the book" +] + +keys = ["Title", "Author", "Publication_date", "Date_read"] + +book = {} + +for key, prompt in zip(keys,prompts): + book[key] = input(prompt + ": ") + +``` + +Here I take the `prompts` and `keys` lists, collate them via `zip` and then loop through them to populate the `book` dictionary, which contains properties sourced from the `keys` list and values from the user's answers to the `prompts`.