PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Python · Theory

List Comprehension in Python

← All stacks

Theory

47/268

List Comprehension in Python

List comprehension builds a new list in one expression. Shape: [n * n for n in nums if n % 2 == 1]. Same result as a for-loop plus append, shorter when the idea is simple. squares = [x * x for x in range(5)] → [0, 1, 4, 9, 16]. Optional if filters items.

Don’t nest three comprehensions. If you cannot say it in one breath, use a loop. Write the comprehension and the equivalent for-loop in the viva — same output, two styles.

List Comprehension in Python — output — [1, 9, 25]. Odds 1,3,5 squared.

Diagram
nums = [1, 2, 3, 4, 5]
         │ n*n if odd
         ▼
      [1, 9, 25]
Exam tip

Comprehension vs loop — same result.

Example

# List comprehension
nums = [1, 2, 3, 4, 5]
print([n * n for n in nums if n % 2 == 1])

List Comprehension in Python — output: [1, 9, 25]. Odds 1,3,5 squared.

Short notes

  • Def[expr for x in seq if cond].
  • RuleBuilds a new list.
  • TrapOver-nested comprehensions.

Questions

1

Explain List Comprehension as if you are teaching a junior — definition, then one tiny script.

2

What does the example print, and why?

3

What mistake do freshers make with List Comprehension?

Previous← Python ModulesNextcollections module →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.