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

Writing concise code with zip and enumerate - Python Tutorial

From the course: Python for JavaScript Developers

Start my 1-month free trial

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…

Contents