- Microsoft 365 Outage: Outlook, Teams and Copilot Down. What We Know
- Diwali Online Shopping Scams 2026: 10 Ways to Stay Safe
- 10 Best DDoS Protection Services for 2027
- 12 Best Web Application Firewalls (WAF) for 2027
- Best Ethical Hacking Tools in 2027: Free & Paid Picks
- Software supply chain attacks 2026: Timeline and developer checklist
- Binance Hack: $6.3 Billion Drained in the Largest Crypto Theft Ever. What We Know So Far
- This Site Uses XSLT Chrome Warning Explained: How to Fix It
List comprehension
List comprehensions are an elegant way to create new lists in Python. List comprehension is faster and more compact than normal functions and loops for creating a list in Python.
Syntax of List Comprehension
[expression for item in list]
List Comprehension vs For Loop in Python
Suppose, we want to separate the letters of the word animal and add the letters as items of a list. The first thing that comes to mind could be using for loop.
Example 1: using for loop
letters = []
for i in 'animal':
letters.append(i)
print(letters)When we run the program, the output will be:
['a', 'n', 'i', 'm', 'a', 'l']
Python has an easier way to solve this issue using List Comprehension.
Example 2: using list comprehension
letters = [ letter for letter in 'animal' ] print(letters)
In the above example, a new list is assigned to variable letters, and the list contains the items of the iterable string ‘animal’. We call print() function to receive the output.
List comprehension is generally more compact and faster than normal functions and loops for creating lists.



