A Python list comprehension is a method for creating lists based on other lists, sequences, or iterators.
- [Instructor] A list comprehension is a list…created based on another list or iterator.…Here in Komodo, I've opened a working copy…of comprehension.py, from chapter eight…of the exercise files.…This list comprehension is a very common technique…in Python, so it's good to understand it.…Here on line five, I've created a sequence,…based on the range function, the range generator,…so it will be zero to 10.…If I save and run this,…you'll see it says zero through 10 in our output.…
I have this little print_list,…which is for printing our sequence.…And let's go ahead and create a list comprehension.…I'll create a second sequence, I'll call it seq2.…And this one will be a list and it'll be a list…of each of the elements of our first list,…multiplied by two.…So I'll just say x times two for x in seq.…It's as simple as that.…And when I go ahead and print this…with our print_list, you'll see that…it prints each of the elements of the first list…multiplied by two.…
So we now have a separate sequence,…which is the first sequence multiplied by two.…
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficiently (2017)with Michele Vallisneri2h 15m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.