SlideShare a Scribd company logo
2
Most read
3
Most read
Python List
Comprehensions
‘Traditional’ way
Constructing a list of squares of even numbers:
squares_of_evens = []
# range(10) -> [0,1,2,3,4,5,6,7,8,9]
for n in range(10):
if n%2==0:
squares_of_evens.append(n*n)
print squares_of_evens #[0, 4, 16, 36, 64]
It works - but ugly and slow!
Construct lists in a concise way
General syntax:
new_list = [new_item for item in input_list]
new_list = [new_item for item in input_list if some_condition]
Squares of even numbers:
squares_of_evens = [n*n for n in range(10) if (n%2 == 0)]
print squares_of_evens #[0, 4, 16, 36, 64]
Thanks!

More Related Content

PDF
Python list
PDF
Data type list_methods_in_python
PPTX
List in Python
PPTX
Python list
PPTX
LIST IN PYTHON
PDF
List , tuples, dictionaries and regular expressions in python
PDF
Python Variable Types, List, Tuple, Dictionary
PDF
Set methods in python
Python list
Data type list_methods_in_python
List in Python
Python list
LIST IN PYTHON
List , tuples, dictionaries and regular expressions in python
Python Variable Types, List, Tuple, Dictionary
Set methods in python

What's hot (20)

PPTX
Unit 4 python -list methods
PDF
Python set
PDF
List,tuple,dictionary
PDF
Arrays in python
PDF
Python programming : List and tuples
PPTX
Python PCEP Lists Collections of Data
PPTX
Dictionaries and Sets in Python
PDF
Joc live session
PPTX
Data Structures in Python
PPTX
Python Programming Essentials - M12 - Lists
PPTX
Python programming -Tuple and Set Data type
PDF
Python Workshop Part 2. LUG Maniapl
PDF
Arrays In Python | Python Array Operations | Edureka
PPTX
Chapter 15 Lists
PDF
Python tuples and Dictionary
PPTX
Basic data structures in python
PDF
Unit 4 python -list methods
Python set
List,tuple,dictionary
Arrays in python
Python programming : List and tuples
Python PCEP Lists Collections of Data
Dictionaries and Sets in Python
Joc live session
Data Structures in Python
Python Programming Essentials - M12 - Lists
Python programming -Tuple and Set Data type
Python Workshop Part 2. LUG Maniapl
Arrays In Python | Python Array Operations | Edureka
Chapter 15 Lists
Python tuples and Dictionary
Basic data structures in python
Ad

Viewers also liked (6)

PDF
GraphQL in an Age of REST
PDF
Let’s Learn Python An introduction to Python
PPT
Introduction to Python
PDF
Learn 90% of Python in 90 Minutes
PPT
Introduction to Python
GraphQL in an Age of REST
Let’s Learn Python An introduction to Python
Introduction to Python
Learn 90% of Python in 90 Minutes
Introduction to Python
Ad

More from Yos Riady (9)

PDF
Brief introduction to Serverless (2018)
PDF
Type Checking in Javascript with Flow
PDF
Schema-First API Design
PDF
Writing Domain Specific Languages with JSON Schema
PDF
Ruby on Rails Workshop
PDF
Online Payments and You
PDF
Entity Component Systems
PDF
Introduction to React
PPTX
Intro to Web Map APIs
Brief introduction to Serverless (2018)
Type Checking in Javascript with Flow
Schema-First API Design
Writing Domain Specific Languages with JSON Schema
Ruby on Rails Workshop
Online Payments and You
Entity Component Systems
Introduction to React
Intro to Web Map APIs

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
AI in Product Development-omnex systems
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
history of c programming in notes for students .pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PDF
top salesforce developer skills in 2025.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Wondershare Filmora 15 Crack With Activation Key [2025
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
How Creative Agencies Leverage Project Management Software.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Migrate SBCGlobal Email to Yahoo Easily
ManageIQ - Sprint 268 Review - Slide Deck
AI in Product Development-omnex systems
Softaken Excel to vCard Converter Software.pdf
PTS Company Brochure 2025 (1).pdf.......
Odoo Companies in India – Driving Business Transformation.pdf
history of c programming in notes for students .pptx
Design an Analysis of Algorithms I-SECS-1021-03
System and Network Administraation Chapter 3
top salesforce developer skills in 2025.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
CHAPTER 2 - PM Management and IT Context
Odoo POS Development Services by CandidRoot Solutions
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Adobe Illustrator 28.6 Crack My Vision of Vector Design

Python List Comprehensions

  • 2. ‘Traditional’ way Constructing a list of squares of even numbers: squares_of_evens = [] # range(10) -> [0,1,2,3,4,5,6,7,8,9] for n in range(10): if n%2==0: squares_of_evens.append(n*n) print squares_of_evens #[0, 4, 16, 36, 64] It works - but ugly and slow!
  • 3. Construct lists in a concise way General syntax: new_list = [new_item for item in input_list] new_list = [new_item for item in input_list if some_condition] Squares of even numbers: squares_of_evens = [n*n for n in range(10) if (n%2 == 0)] print squares_of_evens #[0, 4, 16, 36, 64]