From the course: Python for JavaScript Developers
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Writing concise code with zip and enumerate - Python Tutorial
From the course: Python for JavaScript Developers
Writing concise code with zip and enumerate
- [Instructor] Let's take a look at a few built-in utilities that can help us iterate more cleanly and efficiently. So here I am at 05_02.py, and right on the top, on line one and two, I define two lists. The first one is NAMES and that's a list of strings, and the second one is AGES and that's a list of integers. Now, on line six, you'll see that I iterate over the ages using the reversed function. So, for num in reversed AGES. And this allows me to iterate starting at the end of the list. Now, reversed doesn't create a new list in memory. On line 11, you'll see that I iterate over AGES again, but this time using the sorted function. Sorted actually creates a new sorted list in memory. What if I wanted to iterate two things at the same time? The zip function allows me to do just that. On line 16, you see that I say, for name, age in zip, and you can think of zip as bringing these together, NAMES, AGES. So now, each…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Iterating in Python3m 46s
-
(Locked)
Writing concise code with zip and enumerate3m 39s
-
(Locked)
From JavaScript map and filter to an elegant Python comprehensions6m 2s
-
(Locked)
Decorators are essential3m 35s
-
(Locked)
Challenge: Map to comprehension5m 19s
-
(Locked)
Solution: Map to comprehension2m 32s
-
(Locked)
-