SlideShare a Scribd company logo
http://www.skillbrew.com
/Skillbrew
Talent brewed by the industry itself
List Comprehensions
Pavan Verma
@YinYangPavan
Founder, P3 InfoTech Solutions Pvt. Ltd.
1
Python Programming Essentials
© SkillBrew http://skillbrew.com
Contents
 Introduction
 Syntax
 filter
 Nested loops
2
© SkillBrew http://skillbrew.com
Introduction
Lets say you are given a list and your task is to generate
another list of cubes
sample = [10, 20, 30, 40]
cubes = []
for x in sample:
cubes.append(x**3)
print cubes
Output:
[1000, 8000, 27000, 64000]
3
© SkillBrew http://skillbrew.com
Lets write a List comprehension
sample = [10, 20, 30, 40]
cubes = [ x**3 for x in sample_list]
print cubes
Output:
[1000, 8000, 27000, 64000]
4
sample = [10, 20, 30, 40]
cubes = []
for x in sample:
cubes.append(x**3)
vs
© SkillBrew http://skillbrew.com
Syntax
cubes = [ x**3 for x in sample_list]
Expression For loop
5
© SkillBrew http://skillbrew.com
Filter
Lets tweak the problem a little. Generate a list of cubes but
do the operation only on elements that are even.
sample = [10, 21, 33, 40]
cubes = [ x**3 for x in sample if x%2 == 0 ]
print cubes
Output:
[1000, 64000]
6
© SkillBrew http://skillbrew.com
Nested for loops
Lets say you have a 3 3 Matrix and you have to print
out all the coordinate pairs
0 1 2
0 (0,0) (0, 1) (0, 2)
1 (1,0) (1, 1) (1, 2)
2 (2,0) (2, 1) (2, 2)
7
© SkillBrew http://skillbrew.com
Naive way
matrix = []
for x in range(3):
for y in range(3):
matrix.append((x,y))
print matrix
Output:
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1),
(1, 2), (2, 0), (2, 1), (2, 2)]
8
© SkillBrew http://skillbrew.com
With List comprehensions
matrix = [(x,y) for x in range(3) for y in
range(3)]
print matrix
Output:
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1,
2), (2, 0), (2, 1), (2, 2)]
9
© SkillBrew http://skillbrew.com
Takeaways
1. You have to write less code
2. Keeps the code clean as the syntax is precise and
a one-liner
10
© SkillBrew http://skillbrew.com
Resources
 http://www.pythonforbeginners.com/list
s/list-comprehensions-in-python/
11
12

More Related Content

PPTX
Python Programming Essentials - M17 - Functions
PDF
Advanced python
PPT
Cpp tutorial
PDF
Docopt
PDF
Advanced Python, Part 1
PDF
Imugi: Compiler made with Python
PPTX
INTRODUCTION TO FUNCTIONS IN PYTHON
PDF
A tour of Python
Python Programming Essentials - M17 - Functions
Advanced python
Cpp tutorial
Docopt
Advanced Python, Part 1
Imugi: Compiler made with Python
INTRODUCTION TO FUNCTIONS IN PYTHON
A tour of Python

What's hot (20)

DOCX
Arrry structure Stacks in data structure
PDF
Scalapeno18 - Thinking Less with Scala
PPT
friends functionToshu
PPTX
USE OF PRINT IN PYTHON PART 2
PPT
Operator overloading
PDF
Introduction to ad-3.4, an automatic differentiation library in Haskell
PDF
Ds lab handouts
PPTX
#OOP_D_ITS - 2nd - C++ Getting Started
PPTX
Unit2 input output
PPT
Introduction to cython
PDF
Advanced Python, Part 2
PPTX
Dynamic memory allocation in c++
PPTX
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
PPTX
Python programming workshop session 1
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
PPT
Python Part 1
PPTX
Ruby on rails tips
TXT
Advance C++notes
PPTX
Arrry structure Stacks in data structure
Scalapeno18 - Thinking Less with Scala
friends functionToshu
USE OF PRINT IN PYTHON PART 2
Operator overloading
Introduction to ad-3.4, an automatic differentiation library in Haskell
Ds lab handouts
#OOP_D_ITS - 2nd - C++ Getting Started
Unit2 input output
Introduction to cython
Advanced Python, Part 2
Dynamic memory allocation in c++
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Python programming workshop session 1
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Part 1
Ruby on rails tips
Advance C++notes
Ad

Viewers also liked (20)

PPTX
Python Programming Essentials - M44 - Overview of Web Development
KEY
The One Way
KEY
PyCon Philippines 2012 Keynote
PDF
Intro to Data Visualizations
KEY
Lighting talk on django-social-auth
PPTX
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
PPTX
Python Programming Essentials - M25 - os and sys modules
PPTX
Python Programming Essentials - M29 - Python Interpreter and Files
PPTX
Python Programming Essentials - M35 - Iterators & Generators
KEY
Round pegs and square holes
PPTX
Python Programming Essentials - M39 - Unit Testing
PDF
An Extreme Talk about the Zen of Python
PPTX
Python Programming Essentials - M40 - Invoking External Programs
PPTX
Python Programming Essentials - M28 - Debugging with pdb
PPTX
Python Programming Essentials - M31 - PEP 8
PPTX
Python Programming Essentials - M5 - Variables
PDF
How to Write a Popular Python Library by Accident
PDF
Thinking hard about_python
PDF
Intro to Python
Python Programming Essentials - M44 - Overview of Web Development
The One Way
PyCon Philippines 2012 Keynote
Intro to Data Visualizations
Lighting talk on django-social-auth
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M35 - Iterators & Generators
Round pegs and square holes
Python Programming Essentials - M39 - Unit Testing
An Extreme Talk about the Zen of Python
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M28 - Debugging with pdb
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M5 - Variables
How to Write a Popular Python Library by Accident
Thinking hard about_python
Intro to Python
Ad

Similar to Python Programming Essentials - M34 - List Comprehensions (20)

PPTX
The Java Fx Platform – A Java Developer’S Guide
PDF
Computer Science Sample Paper 2015
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PDF
Benchy: Lightweight framework for Performance Benchmarks
ODT
Introduction to biopython
PPTX
Nalinee java
PDF
TDD CrashCourse Part3: TDD Techniques
PDF
Mysql python
PPTX
Mysql python
PDF
Python - Lecture 12
PPTX
PPT on Data Science Using Python
PPT
Lec3
PDF
Auto testing!
PPTX
Tool Command language (TCL) Operators.pptx
PDF
CBSE Question Paper Computer Science with C++ 2011
PDF
lab-assgn-practical-file-xii-cs.pdf
PPT
17-Arrays en java presentación documento
PPTX
PPTX
C sharp 8.0 new features
PPTX
C sharp 8.0 new features
The Java Fx Platform – A Java Developer’S Guide
Computer Science Sample Paper 2015
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Benchy: Lightweight framework for Performance Benchmarks
Introduction to biopython
Nalinee java
TDD CrashCourse Part3: TDD Techniques
Mysql python
Mysql python
Python - Lecture 12
PPT on Data Science Using Python
Lec3
Auto testing!
Tool Command language (TCL) Operators.pptx
CBSE Question Paper Computer Science with C++ 2011
lab-assgn-practical-file-xii-cs.pdf
17-Arrays en java presentación documento
C sharp 8.0 new features
C sharp 8.0 new features

More from P3 InfoTech Solutions Pvt. Ltd. (18)

PPTX
Python Programming Essentials - M27 - Logging module
PPTX
Python Programming Essentials - M24 - math module
PPTX
Python Programming Essentials - M23 - datetime module
PPTX
Python Programming Essentials - M22 - File Operations
PPTX
Python Programming Essentials - M21 - Exception Handling
PPTX
Python Programming Essentials - M20 - Classes and Objects
PPTX
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
PPTX
Python Programming Essentials - M18 - Modules and Packages
PPTX
Python Programming Essentials - M15 - References
PPTX
Python Programming Essentials - M14 - Dictionaries
PPTX
Python Programming Essentials - M13 - Tuples
PPTX
Python Programming Essentials - M12 - Lists
PPTX
Python Programming Essentials - M11 - Comparison and Logical Operators
PPTX
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
PPTX
Python Programming Essentials - M9 - String Formatting
PPTX
Python Programming Essentials - M8 - String Methods
PPTX
Python Programming Essentials - M7 - Strings
PPTX
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M24 - math module
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M18 - Modules and Packages
Python Programming Essentials - M15 - References
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M13 - Tuples
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M8 - String Methods
Python Programming Essentials - M7 - Strings
Python Programming Essentials - M6 - Code Blocks and Indentation

Python Programming Essentials - M34 - List Comprehensions