From d38e81c37ecc1cb9a8ddf300e3a4cad571456d82 Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Tue, 6 Sep 2022 12:01:49 +0530 Subject: [PATCH 1/8] Write documentation for zip & enumerate --- basics/detailed_zip_&_enumerate.md | 144 ++++++++++++++++++++++++ basics/trey-hunner-zip-and-enumerate.md | 2 + 2 files changed, 146 insertions(+) create mode 100644 basics/detailed_zip_&_enumerate.md diff --git a/basics/detailed_zip_&_enumerate.md b/basics/detailed_zip_&_enumerate.md new file mode 100644 index 0000000..8ab6bf9 --- /dev/null +++ b/basics/detailed_zip_&_enumerate.md @@ -0,0 +1,144 @@ +# zip and enumerate in detail + +## zip + +What comes to your mind when you hear the word `zip`? A mechanism extensively used to tie two parts of something i.e. shirt, Jackect etc. The `zip()` functions does pretty much same, it helps us tie two or more different items together. + +### Syntax + +`zip(iterator1, iterator2, iterator3 ...) ` + +### Parameter Values + +`zip()` can take 2 or more iterator values like so `zip(iterator1, iterator2, iterator3....)`. These +iterators will be joined together once the function is executed. + +### Example 1 + +Let's look at an example, suppose we wish to join 2 tuples together. + +```python +users = ("Tushar", "Aman", "Anurag", "Sohit") +uids = ("usr122", "usr123", "usr124", "usr125") +user_details = zip(uids, users) +print(user_details) +``` + +### Output 1 + +```shell + +``` + +If we want to get some real output we can convert this into List, tuple or a set. + +### Example 2 + +```python +users = ("Tushar", "Aman", "Anurag", "Sohit") +uids = ("usr122", "usr123", "usr124", "usr125") +user_details = zip(uids, users) +print(list(user_details)) # converting to list +``` + +### Output 2 + +```shell +[('usr122', 'Tushar'), ('usr123', 'Aman'), ('usr124', 'Anurag'), ('usr125', 'Sohit')] +``` + +The keen-eyed amongst you might ask what if these iterators were of different lengths? well, then the ones with the extra fields will be ignored like so. + +### Example 3 + +```python +users = ("Tushar", "Aman", "Anurag", "Sohit") +uids = ("usr122", "usr123", "usr124") +emails = ("tushar@example.com", "aman@example.com", "anurag@example.com", "sohit@example.com") +user_details = zip(uids, users, emails) +print(list(user_details)) +``` + +### Output 3 + +```shell +[('usr122', 'Tushar', 'tushar@example.com'), ('usr123', 'Aman', 'aman@example.com'), ('usr124', 'Anurag', 'anurag@example.com')] +``` + +## enumerate + +`enumerate()` is an amazing Built-in function offered by python. When used, gives us the index and the item combined. long story short! `enumerate` comes in handy while working with loops as it gives us access to both the item and it's index at the same time. + +### Syntax + +`enumerate(iterable, start=0)` + +### Parameters + +* Iterable: any iteration supporting object +* Start: Starting index value, default is 0 + +### Example 1: basic enumerate example + +```python +even_nums = [2, 4, 6, 8, 10, 12] +print(list(enumerate(even_nums))) +``` +### Output 1 + +```shell +[(0, 2), (1, 4), (2, 6), (3, 8), (4, 10), (5, 12)] +``` + +Now before we use it in a loop let's take a look how we would do the same thing in a traditional way. + +### Example 2: looping without enumerate + +```python +even_nums = [2, 4, 6, 8, 10, 12] +for index in range(0, len(even_nums)): + print(f"Index of {even_nums[index]} is {index}") +``` + +### Output 2 + +```shell +Index of 2 is 0 +Index of 4 is 1 +Index of 6 is 2 +Index of 8 is 3 +Index of 10 is 4 +Index of 12 is 5 +``` + +### Example 3: looping with `enumerate()` + +```python +even_nums = [2, 4, 6, 8, 10, 12] +for index, item in enumerate(even_nums): + print(f"Index of {item} is {index}") +``` + +### Output 2 + +```shell +Index of 2 is 0 +Index of 4 is 1 +Index of 6 is 2 +Index of 8 is 3 +Index of 10 is 4 +Index of 12 is 5 +``` + +*** + +If you have trouble with this tutorial please [tell me about +it](../contact-me.md) and I'll make this tutorial better. If you +like this tutorial, please [give it a +star](../README.md#how-can-i-thank-you-for-writing-and-sharing-this-tutorial). + +You may use this tutorial freely at your own risk. See +[LICENSE](../LICENSE). + +[Previous](loops.md) | [Next](dicts.md) | +[List of contents](../README.md#basics) diff --git a/basics/trey-hunner-zip-and-enumerate.md b/basics/trey-hunner-zip-and-enumerate.md index 3a211de..dd3b1fc 100644 --- a/basics/trey-hunner-zip-and-enumerate.md +++ b/basics/trey-hunner-zip-and-enumerate.md @@ -1,5 +1,7 @@ # Trey Hunner: zip and enumerate +> Find the detailed Zip and Enumerate Tutorial [here!](detailed_zip_&_enumerate.md) + Now we know how [for loops](loops.md#for-loops) work in Python. But for loops aren't limited to printing each item in a list, they can do a lot more. From 68a43d4d0bc9383505b51d002bb1ceecf12b423c Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Wed, 7 Sep 2022 10:34:38 +0530 Subject: [PATCH 2/8] write zip and enumerate in existing trey-hunner-zip-and-enumerate.md and a few minor changes --- basics/detailed_zip_&_enumerate.md | 144 ------------------ basics/trey-hunner-zip-and-enumerate.md | 191 ++++++++++++++++-------- 2 files changed, 132 insertions(+), 203 deletions(-) delete mode 100644 basics/detailed_zip_&_enumerate.md diff --git a/basics/detailed_zip_&_enumerate.md b/basics/detailed_zip_&_enumerate.md deleted file mode 100644 index 8ab6bf9..0000000 --- a/basics/detailed_zip_&_enumerate.md +++ /dev/null @@ -1,144 +0,0 @@ -# zip and enumerate in detail - -## zip - -What comes to your mind when you hear the word `zip`? A mechanism extensively used to tie two parts of something i.e. shirt, Jackect etc. The `zip()` functions does pretty much same, it helps us tie two or more different items together. - -### Syntax - -`zip(iterator1, iterator2, iterator3 ...) ` - -### Parameter Values - -`zip()` can take 2 or more iterator values like so `zip(iterator1, iterator2, iterator3....)`. These -iterators will be joined together once the function is executed. - -### Example 1 - -Let's look at an example, suppose we wish to join 2 tuples together. - -```python -users = ("Tushar", "Aman", "Anurag", "Sohit") -uids = ("usr122", "usr123", "usr124", "usr125") -user_details = zip(uids, users) -print(user_details) -``` - -### Output 1 - -```shell - -``` - -If we want to get some real output we can convert this into List, tuple or a set. - -### Example 2 - -```python -users = ("Tushar", "Aman", "Anurag", "Sohit") -uids = ("usr122", "usr123", "usr124", "usr125") -user_details = zip(uids, users) -print(list(user_details)) # converting to list -``` - -### Output 2 - -```shell -[('usr122', 'Tushar'), ('usr123', 'Aman'), ('usr124', 'Anurag'), ('usr125', 'Sohit')] -``` - -The keen-eyed amongst you might ask what if these iterators were of different lengths? well, then the ones with the extra fields will be ignored like so. - -### Example 3 - -```python -users = ("Tushar", "Aman", "Anurag", "Sohit") -uids = ("usr122", "usr123", "usr124") -emails = ("tushar@example.com", "aman@example.com", "anurag@example.com", "sohit@example.com") -user_details = zip(uids, users, emails) -print(list(user_details)) -``` - -### Output 3 - -```shell -[('usr122', 'Tushar', 'tushar@example.com'), ('usr123', 'Aman', 'aman@example.com'), ('usr124', 'Anurag', 'anurag@example.com')] -``` - -## enumerate - -`enumerate()` is an amazing Built-in function offered by python. When used, gives us the index and the item combined. long story short! `enumerate` comes in handy while working with loops as it gives us access to both the item and it's index at the same time. - -### Syntax - -`enumerate(iterable, start=0)` - -### Parameters - -* Iterable: any iteration supporting object -* Start: Starting index value, default is 0 - -### Example 1: basic enumerate example - -```python -even_nums = [2, 4, 6, 8, 10, 12] -print(list(enumerate(even_nums))) -``` -### Output 1 - -```shell -[(0, 2), (1, 4), (2, 6), (3, 8), (4, 10), (5, 12)] -``` - -Now before we use it in a loop let's take a look how we would do the same thing in a traditional way. - -### Example 2: looping without enumerate - -```python -even_nums = [2, 4, 6, 8, 10, 12] -for index in range(0, len(even_nums)): - print(f"Index of {even_nums[index]} is {index}") -``` - -### Output 2 - -```shell -Index of 2 is 0 -Index of 4 is 1 -Index of 6 is 2 -Index of 8 is 3 -Index of 10 is 4 -Index of 12 is 5 -``` - -### Example 3: looping with `enumerate()` - -```python -even_nums = [2, 4, 6, 8, 10, 12] -for index, item in enumerate(even_nums): - print(f"Index of {item} is {index}") -``` - -### Output 2 - -```shell -Index of 2 is 0 -Index of 4 is 1 -Index of 6 is 2 -Index of 8 is 3 -Index of 10 is 4 -Index of 12 is 5 -``` - -*** - -If you have trouble with this tutorial please [tell me about -it](../contact-me.md) and I'll make this tutorial better. If you -like this tutorial, please [give it a -star](../README.md#how-can-i-thank-you-for-writing-and-sharing-this-tutorial). - -You may use this tutorial freely at your own risk. See -[LICENSE](../LICENSE). - -[Previous](loops.md) | [Next](dicts.md) | -[List of contents](../README.md#basics) diff --git a/basics/trey-hunner-zip-and-enumerate.md b/basics/trey-hunner-zip-and-enumerate.md index dd3b1fc..6cab40e 100644 --- a/basics/trey-hunner-zip-and-enumerate.md +++ b/basics/trey-hunner-zip-and-enumerate.md @@ -1,6 +1,4 @@ -# Trey Hunner: zip and enumerate - -> Find the detailed Zip and Enumerate Tutorial [here!](detailed_zip_&_enumerate.md) +# zip and enumerate Now we know how [for loops](loops.md#for-loops) work in Python. But for loops aren't limited to printing each item in a list, they can @@ -77,63 +75,138 @@ b 2 c 3 >>> ``` +[comment]: # (Zip and Enumerarate by Tushar starts here) + +## zip + +What comes to your mind when you hear the word `zip`? A mechanism extensively used to tie two parts of something i.e. Shirt, Jacket etc. The `zip()` functions does pretty much same, it helps us tie two or more different items together. + +### Syntax + +`zip(iterator1, iterator2, iterator3 ...) ` + +### Parameter Values + +`zip()` can take 2 or more iterator values like so `zip(iterator1, iterator2, iterator3....)`. These +iterators will be joined together once the function is executed. + +### Example 1 + +Let's look at an example, suppose we wish to join 2 tuples together. + +```python +>>> users = ("Tushar", "Aman", "Anurag", "Sohit") +uids = ("usr122", "usr123", "usr124", "usr125") +user_details = zip(uids, users) +print(user_details) +>>> + +>>> +``` + +If we want to get some real output we can convert this zip object into List, tuple or a set. + +### Example 2 + +```python +>>> users = ("Tushar", "Aman", "Anurag", "Sohit") +uids = ("usr122", "usr123", "usr124", "usr125") +user_details = zip(uids, users) +print(list(user_details)) # converting to list +>>> +[('usr122', 'Tushar'), ('usr123', 'Aman'), ('usr124', 'Anurag'), ('usr125', 'Sohit')] +>>> +``` + +What if these iterators were of different lengths? well, then the ones with the extra fields will be ignored like so. + +### Example 3 + +```python +>>> users = ("Tushar", "Aman", "Anurag", "Sohit") +uids = ("usr122", "usr123", "usr124") +emails = ("tushar@example.com", "aman@example.com", "anurag@example.com", "sohit@example.com") +user_details = zip(uids, users, emails) +print(list(user_details)) +>>> +[('usr122', 'Tushar', 'tushar@example.com'), ('usr123', 'Aman', 'aman@example.com'), ('usr124', 'Anurag', 'anurag@example.com')] +>>> +``` + +Length of `uids` is 3 thus it is the shortest tuple, now `zip()` function will crop other tuples to consider them equally and will finally join all of them together. + +### Example 4: Using zip with for loop + +```python +>>> roll_nums = (20, 25, 28) +students = ("Joe", "Max", "Michel") +>>> for roll_num, student in zip(roll_nums, students): +... print(f"Roll number of {student} is {roll_num}") +... +Roll number of Joe is 20 +Roll number of Max is 25 +Roll number of Michel is 28 +>>> +``` + +## enumerate + +`enumerate()` is an amazing Built-in function offered by python. When used, gives us the index and the item combined. long story short! `enumerate` comes in handy while working with loops as it gives us access to both the item and it's index at the same time. + +### Syntax + +`enumerate(iterable, start=0)` + +### Parameters + +* Iterable: any iteration supporting object +* Start: Starting index value, default is 0 + +### Example 1: basic enumerate example + +```python +>>> even_nums = [2, 4, 6, 8, 10, 12] +print(list(enumerate(even_nums))) +>>> +[(0, 2), (1, 4), (2, 6), (3, 8), (4, 10), (5, 12)] +>>> +``` + +Now before we use it in a loop let's take a look how we would do the same thing in a traditional way. + +### Example 2: looping without `enumerate()` + +```python +>>> even_nums = [2, 4, 6, 8, 10, 12] +>>> for index in range(0, len(even_nums)): +... print(f"Index of {even_nums[index]} is {index}") +... +Index of 2 is 0 +Index of 4 is 1 +Index of 6 is 2 +Index of 8 is 3 +Index of 10 is 4 +Index of 12 is 5 +>>> +``` + +### Example 3: looping with `enumerate()` + +```python +>>> even_nums = [2, 4, 6, 8, 10, 12] +>>> for index, item in enumerate(even_nums): +... print(f"Index of {item} is {index}") +... +Index of 2 is 0 +Index of 4 is 1 +Index of 6 is 2 +Index of 8 is 3 +Index of 10 is 4 +Index of 12 is 5 +>>> +``` -Now you're ready to read [this awesome looping -tutorial](http://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/). -Read it now, then come back here and do the exercises. - -## Exercises - -1. Create a program that works like this. Here I entered everything - after the `>` prompt that the program displayed. - - ``` - Enter something, and press Enter without typing anything when you're done. - >hello there - >this is a test - >it seems to work - > - Line 1 is: hello there - Line 2 is: this is a test - Line 3 is: it seems to work - ``` - -2. Create a program that prints all letters from A to Z and a to z - next to each other: - - ``` - A a - B b - C c - ... - X x - Y y - Z z - ``` - - Start your program like this: - - ```python - uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' - lowercase = 'abcdefghijklmnopqrstuvwxyz' - ``` - - **Hint:** how do strings behave with `zip`? Try it out on the - `>>>` prompt and see. - -3. Can you make it print the indexes also? - - ``` - 1 A a - 2 B b - 3 C c - ... - 24 X x - 25 Y y - 26 Z z - ``` - -The answers are [here](answers.md). +[comment]: # (Zip and Enumerarate by Tushar ends here) *** From a229e392a9fdc1cf8b89a30e50e130808a844715 Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Wed, 7 Sep 2022 19:22:57 +0530 Subject: [PATCH 3/8] Remove unnecessary code and modify trey-hunner-zip-and-enumerate.md --- basics/trey-hunner-zip-and-enumerate.md | 139 ++++++++++++++---------- 1 file changed, 80 insertions(+), 59 deletions(-) diff --git a/basics/trey-hunner-zip-and-enumerate.md b/basics/trey-hunner-zip-and-enumerate.md index 6cab40e..38bdf47 100644 --- a/basics/trey-hunner-zip-and-enumerate.md +++ b/basics/trey-hunner-zip-and-enumerate.md @@ -75,71 +75,49 @@ b 2 c 3 >>> ``` -[comment]: # (Zip and Enumerarate by Tushar starts here) ## zip What comes to your mind when you hear the word `zip`? A mechanism extensively used to tie two parts of something i.e. Shirt, Jacket etc. The `zip()` functions does pretty much same, it helps us tie two or more different items together. -### Syntax - -`zip(iterator1, iterator2, iterator3 ...) ` - -### Parameter Values - -`zip()` can take 2 or more iterator values like so `zip(iterator1, iterator2, iterator3....)`. These -iterators will be joined together once the function is executed. - -### Example 1 - -Let's look at an example, suppose we wish to join 2 tuples together. - ```python ->>> users = ("Tushar", "Aman", "Anurag", "Sohit") -uids = ("usr122", "usr123", "usr124", "usr125") -user_details = zip(uids, users) -print(user_details) ->>> - +>>> users = ["Tushar", "Aman", "Anurag", "Sohit"] +>>> uids = ["usr122", "usr123", "usr124", "usr125"] +>>> user_details = zip(uids, users) +>>> print(user_details) + >>> ``` -If we want to get some real output we can convert this zip object into List, tuple or a set. - -### Example 2 +After calling zip, an iterator is returned. In order to see the content wrapped inside, we need to first convert it to a list. Basically, `zip()` is an iterator, i.e. lazy: it gives the items as needed, instead of calculating them and storing them into memory all at once. So the zip object cannot show its elements before the elements are used, because it hasn't computed them yet. ```python ->>> users = ("Tushar", "Aman", "Anurag", "Sohit") -uids = ("usr122", "usr123", "usr124", "usr125") -user_details = zip(uids, users) -print(list(user_details)) # converting to list ->>> +>>> users = ["Tushar", "Aman", "Anurag", "Sohit"] +>>> uids = ["usr122", "usr123", "usr124", "usr125"] +>>> user_details = zip(uids, users) +>>> print(list(user_details)) [('usr122', 'Tushar'), ('usr123', 'Aman'), ('usr124', 'Anurag'), ('usr125', 'Sohit')] >>> ``` -What if these iterators were of different lengths? well, then the ones with the extra fields will be ignored like so. - -### Example 3 +If the lists are of different lengths, some items from the end of the longer list will be ignored. ```python ->>> users = ("Tushar", "Aman", "Anurag", "Sohit") -uids = ("usr122", "usr123", "usr124") -emails = ("tushar@example.com", "aman@example.com", "anurag@example.com", "sohit@example.com") -user_details = zip(uids, users, emails) -print(list(user_details)) ->>> -[('usr122', 'Tushar', 'tushar@example.com'), ('usr123', 'Aman', 'aman@example.com'), ('usr124', 'Anurag', 'anurag@example.com')] +>>> users = ["Tushar", "Aman", "Anurag", "Sohit"] +>>> emails = ["tushar@example.com", "aman@example.com", "anurag@example.com", "sohit@example.com"] +>>> users_contact = zip(users, emails) +>>> print(list(users_contact)) +[('Tushar', 'tushar@example.com'), ('Aman', 'aman@example.com'), ('Anurag', 'anurag@example.com')] >>> ``` -Length of `uids` is 3 thus it is the shortest tuple, now `zip()` function will crop other tuples to consider them equally and will finally join all of them together. +Length of `users` list is 3 thus it is the shortest. `zip()` function will crop other lists to consider them equally and will finally join all items together. -### Example 4: Using zip with for loop +### Using zip with for loop ```python ->>> roll_nums = (20, 25, 28) -students = ("Joe", "Max", "Michel") +>>> roll_nums = [20, 25, 28] +>>> students = ["Joe", "Max", "Michel"] >>> for roll_num, student in zip(roll_nums, students): ... print(f"Roll number of {student} is {roll_num}") ... @@ -153,28 +131,16 @@ Roll number of Michel is 28 `enumerate()` is an amazing Built-in function offered by python. When used, gives us the index and the item combined. long story short! `enumerate` comes in handy while working with loops as it gives us access to both the item and it's index at the same time. -### Syntax - -`enumerate(iterable, start=0)` - -### Parameters - -* Iterable: any iteration supporting object -* Start: Starting index value, default is 0 - -### Example 1: basic enumerate example - ```python >>> even_nums = [2, 4, 6, 8, 10, 12] -print(list(enumerate(even_nums))) ->>> +>>> print(list(enumerate(even_nums))) [(0, 2), (1, 4), (2, 6), (3, 8), (4, 10), (5, 12)] >>> ``` -Now before we use it in a loop let's take a look how we would do the same thing in a traditional way. +Before we use it in a loop let's take a look how we would do the same thing in a traditional way. -### Example 2: looping without `enumerate()` +### looping without `enumerate()` ```python >>> even_nums = [2, 4, 6, 8, 10, 12] @@ -190,7 +156,7 @@ Index of 12 is 5 >>> ``` -### Example 3: looping with `enumerate()` +### looping with `enumerate()` ```python >>> even_nums = [2, 4, 6, 8, 10, 12] @@ -206,7 +172,62 @@ Index of 12 is 5 >>> ``` -[comment]: # (Zip and Enumerarate by Tushar ends here) +Now you're ready to read [this awesome looping +tutorial](http://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/). +Read it now, then come back here and do the exercises. + +## Exercises + +1. Create a program that works like this. Here I entered everything + after the `>` prompt that the program displayed. + + ``` + Enter something, and press Enter without typing anything when you're done. + >hello there + >this is a test + >it seems to work + > + Line 1 is: hello there + Line 2 is: this is a test + Line 3 is: it seems to work + ``` + +2. Create a program that prints all letters from A to Z and a to z + next to each other: + + ``` + A a + B b + C c + ... + X x + Y y + Z z + ``` + + Start your program like this: + + ```python + uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + lowercase = 'abcdefghijklmnopqrstuvwxyz' + ``` + + **Hint:** how do strings behave with `zip`? Try it out on the + `>>>` prompt and see. + +3. Can you make it print the indexes also? + + ``` + 1 A a + 2 B b + 3 C c + ... + 24 X x + 25 Y y + 26 Z z + ``` + +The answers are [here](answers.md). *** From 310c1ccbab3cd2593da524045af69dbb42d6c9fb Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Wed, 7 Sep 2022 22:24:02 +0530 Subject: [PATCH 4/8] Make zip and enumerate documentation easy to understand --- basics/trey-hunner-zip-and-enumerate.md | 33 ++++++++++++++----------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/basics/trey-hunner-zip-and-enumerate.md b/basics/trey-hunner-zip-and-enumerate.md index 38bdf47..f0ecfaf 100644 --- a/basics/trey-hunner-zip-and-enumerate.md +++ b/basics/trey-hunner-zip-and-enumerate.md @@ -103,7 +103,7 @@ After calling zip, an iterator is returned. In order to see the content wrapped If the lists are of different lengths, some items from the end of the longer list will be ignored. ```python ->>> users = ["Tushar", "Aman", "Anurag", "Sohit"] +>>> users = ["Tushar", "Aman", "Anurag"] >>> emails = ["tushar@example.com", "aman@example.com", "anurag@example.com", "sohit@example.com"] >>> users_contact = zip(users, emails) >>> print(list(users_contact)) @@ -111,9 +111,13 @@ If the lists are of different lengths, some items from the end of the longer lis >>> ``` -Length of `users` list is 3 thus it is the shortest. `zip()` function will crop other lists to consider them equally and will finally join all items together. +Here the shortest list is `users`, with length 3, so `zip(users, emails)` only takes the first 3 emails. +> We do not recommend calling `zip()` with lists of different lengths. + +### Using zip with `for` loop -### Using zip with for loop +It is very common to `for` loop over a `zip()`, and unpack the returned tuples in the `for` loop. +This is why we introduced unpacking in the beginning of this page. ```python >>> roll_nums = [20, 25, 28] @@ -138,14 +142,12 @@ Roll number of Michel is 28 >>> ``` -Before we use it in a loop let's take a look how we would do the same thing in a traditional way. - -### looping without `enumerate()` +### looping with `enumerate()` ```python >>> even_nums = [2, 4, 6, 8, 10, 12] ->>> for index in range(0, len(even_nums)): -... print(f"Index of {even_nums[index]} is {index}") +>>> for index, item in enumerate(even_nums): +... print(f"Index of {item} is {index}") ... Index of 2 is 0 Index of 4 is 1 @@ -156,12 +158,14 @@ Index of 12 is 5 >>> ``` -### looping with `enumerate()` +### looping without `enumerate()` + +let's take a look at how we can do the same thing in a more traditional way. ```python >>> even_nums = [2, 4, 6, 8, 10, 12] ->>> for index, item in enumerate(even_nums): -... print(f"Index of {item} is {index}") +>>> for index in range(0, len(even_nums)): +... print(f"Index of {even_nums[index]} is {index}") ... Index of 2 is 0 Index of 4 is 1 @@ -172,9 +176,10 @@ Index of 12 is 5 >>> ``` -Now you're ready to read [this awesome looping -tutorial](http://treyhunner.com/2016/04/how-to-loop-with-indexes-in-python/). -Read it now, then come back here and do the exercises. +* `range(0, len(even_nums))` gives 0,1,2,3,4,5, with the list length 6 excluded +* because these are the valid indexes of a list of length 6, `even_nums[index]` prints each element of `even_nums` + +> This is complicated to think about and easy to get wrong, so it's better to use `enumerate()` ## Exercises From 740a5e0db750e151f1f27a4b495286e020a41853 Mon Sep 17 00:00:00 2001 From: Tushar Khatri Date: Sat, 10 Sep 2022 17:12:47 +0530 Subject: [PATCH 5/8] Revisions in trey-hunner-zip-and-enumerate.md --- basics/trey-hunner-zip-and-enumerate.md | 34 +++++++++---------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/basics/trey-hunner-zip-and-enumerate.md b/basics/trey-hunner-zip-and-enumerate.md index f0ecfaf..59d8741 100644 --- a/basics/trey-hunner-zip-and-enumerate.md +++ b/basics/trey-hunner-zip-and-enumerate.md @@ -78,30 +78,27 @@ c 3 ## zip -What comes to your mind when you hear the word `zip`? A mechanism extensively used to tie two parts of something i.e. Shirt, Jacket etc. The `zip()` functions does pretty much same, it helps us tie two or more different items together. +What comes to your mind when you hear the word `zip`? A mechanism extensively used to tie two parts of something, e.g. shirt or jacket. Python's `zip()` functions does pretty much same, it helps us tie corresponding items together. ```python >>> users = ["Tushar", "Aman", "Anurag", "Sohit"] >>> uids = ["usr122", "usr123", "usr124", "usr125"] >>> user_details = zip(uids, users) ->>> print(user_details) - +>>> print(list(user_details)) +[('usr122', 'Tushar'), ('usr123', 'Aman'), ('usr124', 'Anurag'), ('usr125', 'Sohit')] >>> ``` - After calling zip, an iterator is returned. In order to see the content wrapped inside, we need to first convert it to a list. Basically, `zip()` is an iterator, i.e. lazy: it gives the items as needed, instead of calculating them and storing them into memory all at once. So the zip object cannot show its elements before the elements are used, because it hasn't computed them yet. - ```python >>> users = ["Tushar", "Aman", "Anurag", "Sohit"] >>> uids = ["usr122", "usr123", "usr124", "usr125"] >>> user_details = zip(uids, users) ->>> print(list(user_details)) -[('usr122', 'Tushar'), ('usr123', 'Aman'), ('usr124', 'Anurag'), ('usr125', 'Sohit')] +>>> print(user_details) + >>> ``` If the lists are of different lengths, some items from the end of the longer list will be ignored. - ```python >>> users = ["Tushar", "Aman", "Anurag"] >>> emails = ["tushar@example.com", "aman@example.com", "anurag@example.com", "sohit@example.com"] @@ -111,8 +108,9 @@ If the lists are of different lengths, some items from the end of the longer lis >>> ``` + Here the shortest list is `users`, with length 3, so `zip(users, emails)` only takes the first 3 emails. -> We do not recommend calling `zip()` with lists of different lengths. +> We do not recommend calling `zip()` with lists of different lengths, because ignoring items is usually not what you intended to do. ### Using zip with `for` loop @@ -135,15 +133,6 @@ Roll number of Michel is 28 `enumerate()` is an amazing Built-in function offered by python. When used, gives us the index and the item combined. long story short! `enumerate` comes in handy while working with loops as it gives us access to both the item and it's index at the same time. -```python ->>> even_nums = [2, 4, 6, 8, 10, 12] ->>> print(list(enumerate(even_nums))) -[(0, 2), (1, 4), (2, 6), (3, 8), (4, 10), (5, 12)] ->>> -``` - -### looping with `enumerate()` - ```python >>> even_nums = [2, 4, 6, 8, 10, 12] >>> for index, item in enumerate(even_nums): @@ -160,7 +149,7 @@ Index of 12 is 5 ### looping without `enumerate()` -let's take a look at how we can do the same thing in a more traditional way. +It is also possible (but more difficult) to do this without `enumerate()`: ```python >>> even_nums = [2, 4, 6, 8, 10, 12] @@ -176,10 +165,11 @@ Index of 12 is 5 >>> ``` -* `range(0, len(even_nums))` gives 0,1,2,3,4,5, with the list length 6 excluded -* because these are the valid indexes of a list of length 6, `even_nums[index]` prints each element of `even_nums` +Here: +* `range(0, len(even_nums))` gives 0,1,2,3,4,5, with the list length 6 excluded. These are the indexes of our list of length 6. +* `even_nums[index]` prints each element of `even_nums`, because `index` comes from the range of all indexes into that list. -> This is complicated to think about and easy to get wrong, so it's better to use `enumerate()` +Because this is complicated to think about and easy to get wrong, it is better to use `enumerate()`. ## Exercises From b26f5efcb63f28c88fd066d06abfcb01c4385ae8 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sat, 10 Sep 2022 15:56:23 +0300 Subject: [PATCH 6/8] Rename to zip-and-enumerate.md --- basics/{trey-hunner-zip-and-enumerate.md => zip-and-enumerate.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename basics/{trey-hunner-zip-and-enumerate.md => zip-and-enumerate.md} (100%) diff --git a/basics/trey-hunner-zip-and-enumerate.md b/basics/zip-and-enumerate.md similarity index 100% rename from basics/trey-hunner-zip-and-enumerate.md rename to basics/zip-and-enumerate.md From 7ec397d0ac5a655e334cd3d4039286a6155993a3 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sat, 10 Sep 2022 16:00:14 +0300 Subject: [PATCH 7/8] A few tweaks --- basics/zip-and-enumerate.md | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/basics/zip-and-enumerate.md b/basics/zip-and-enumerate.md index 59d8741..07c36ad 100644 --- a/basics/zip-and-enumerate.md +++ b/basics/zip-and-enumerate.md @@ -76,9 +76,12 @@ c 3 >>> ``` +This feature is often used with Python's built-in `zip()` and `enumerate()` functions. + + ## zip -What comes to your mind when you hear the word `zip`? A mechanism extensively used to tie two parts of something, e.g. shirt or jacket. Python's `zip()` functions does pretty much same, it helps us tie corresponding items together. +What comes to your mind when you hear the word `zip`? A mechanism extensively used to tie two parts of something, e.g. shirt or jacket. Python's `zip()` functions does pretty much the same, it helps us tie corresponding items together. ```python >>> users = ["Tushar", "Aman", "Anurag", "Sohit"] @@ -88,14 +91,21 @@ What comes to your mind when you hear the word `zip`? A mechanism extensively us [('usr122', 'Tushar'), ('usr123', 'Aman'), ('usr124', 'Anurag'), ('usr125', 'Sohit')] >>> ``` -After calling zip, an iterator is returned. In order to see the content wrapped inside, we need to first convert it to a list. Basically, `zip()` is an iterator, i.e. lazy: it gives the items as needed, instead of calculating them and storing them into memory all at once. So the zip object cannot show its elements before the elements are used, because it hasn't computed them yet. + +Note that `print(user_details)` doesn't work as expected: + +``` +>>> print(user_details) + +>>> +``` + +This is because `zip()` is an iterator, i.e. lazy: it gives the items as needed, instead of calculating them and storing them into memory all at once like a list. So the zip object cannot show its elements before the elements are used, because it hasn't computed them yet. + ```python >>> users = ["Tushar", "Aman", "Anurag", "Sohit"] >>> uids = ["usr122", "usr123", "usr124", "usr125"] >>> user_details = zip(uids, users) ->>> print(user_details) - ->>> ``` If the lists are of different lengths, some items from the end of the longer list will be ignored. @@ -110,12 +120,13 @@ If the lists are of different lengths, some items from the end of the longer lis Here the shortest list is `users`, with length 3, so `zip(users, emails)` only takes the first 3 emails. -> We do not recommend calling `zip()` with lists of different lengths, because ignoring items is usually not what you intended to do. +We do not recommend calling `zip()` with lists of different lengths, because ignoring items is usually not what you intended to do. -### Using zip with `for` loop +### Using zip in a `for` loop It is very common to `for` loop over a `zip()`, and unpack the returned tuples in the `for` loop. This is why we introduced unpacking in the beginning of this page. +When used this way, there's no need to convert the result of `zip(...)` to a list. ```python >>> roll_nums = [20, 25, 28] @@ -131,7 +142,7 @@ Roll number of Michel is 28 ## enumerate -`enumerate()` is an amazing Built-in function offered by python. When used, gives us the index and the item combined. long story short! `enumerate` comes in handy while working with loops as it gives us access to both the item and it's index at the same time. +`enumerate()` is an amazing Built-in function offered by python. When used, gives us the index and the item combined. ```python >>> even_nums = [2, 4, 6, 8, 10, 12] @@ -147,8 +158,6 @@ Index of 12 is 5 >>> ``` -### looping without `enumerate()` - It is also possible (but more difficult) to do this without `enumerate()`: ```python From 096478510731457b912c79300ff96a615abd75b1 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sat, 10 Sep 2022 16:00:56 +0300 Subject: [PATCH 8/8] Run update-ends.py --- README.md | 2 +- basics/dicts.md | 2 +- basics/loops.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f201dbf..ce76d98 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ to learn more about whatever you want after studying it. 9. [Handy stuff with strings](basics/handy-stuff-strings.md) 10. [Lists and tuples](basics/lists-and-tuples.md) 11. [Loops](basics/loops.md) -12. [Trey Hunner: zip and enumerate](basics/trey-hunner-zip-and-enumerate.md) +12. [zip and enumerate](basics/zip-and-enumerate.md) 13. [Dictionaries](basics/dicts.md) 14. [Defining functions](basics/defining-functions.md) 15. [Writing a larger program](basics/larger-program.md) diff --git a/basics/dicts.md b/basics/dicts.md index 9784c5d..1c271cb 100644 --- a/basics/dicts.md +++ b/basics/dicts.md @@ -332,5 +332,5 @@ star](../README.md#how-can-i-thank-you-for-writing-and-sharing-this-tutorial). You may use this tutorial freely at your own risk. See [LICENSE](../LICENSE). -[Previous](trey-hunner-zip-and-enumerate.md) | [Next](defining-functions.md) | +[Previous](zip-and-enumerate.md) | [Next](defining-functions.md) | [List of contents](../README.md#basics) diff --git a/basics/loops.md b/basics/loops.md index 4e69c60..5412257 100644 --- a/basics/loops.md +++ b/basics/loops.md @@ -501,5 +501,5 @@ star](../README.md#how-can-i-thank-you-for-writing-and-sharing-this-tutorial). You may use this tutorial freely at your own risk. See [LICENSE](../LICENSE). -[Previous](lists-and-tuples.md) | [Next](trey-hunner-zip-and-enumerate.md) | +[Previous](lists-and-tuples.md) | [Next](zip-and-enumerate.md) | [List of contents](../README.md#basics)