Python – List comprehension


List

You can use list to implement stack (ie. LIFO). For that, we can use:

  • append to add to the end of the list
  • pop to remove from the front of the list.

List comprehension (LC)

It is syntactic sugar that transforms one list (any iterable actually) into another list. During this process, elements can be filtered and transformed if needed. In term of performance, LC actually runs faster.

General form:

[ expression for item in list if conditional ]
  • expression can use the variable item
  • there can be nested for loop sequencing in order
  • if-condition is the filter

List comprehension examples

Set and dictionary comprehension


Python – List comprehension

log in

Use demo/demo public access

reset password

Back to
log in
Choose A Format
Personality quiz
Trivia quiz
Poll
Story
List
Meme
Video
Audio
Image