Search
  • English
Login Register
  • Mon - Sat 11.00 am - 8:00 pm
  • 1st-29, Atlanta Business Hub, VIP Road, Surat
  • +91 97129 28220
Code and Debug
  • Offline Courses
    • C Programming
    • Cpp Programming
    • Django Framework
    • Flutter Development
    • HTML & CSS
    • Javascript
    • MySQL
    • Node.js (Core)
    • Node.js (Advance)
    • Python Programming
  • About Us
  • Contact Us
  • Blog
  • Offline Courses
    • C Programming
    • Cpp Programming
    • Django Framework
    • Flutter Development
    • HTML & CSS
    • Javascript
    • MySQL
    • Node.js (Core)
    • Node.js (Advance)
    • Python Programming
  • About Us
  • Contact Us
  • Blog
Code and Debug > Blog > Coding > Python > List Comprehension in Python

List Comprehension in Python

  • November 8, 2022
  • Posted by: Code and Debug
  • Category: Coding Python
2 Comments

Python is known for allowing you to write code that’s simple, easy to write, and almost as easy to read as plain English. One of Python’s most remarkable features is the list comprehension.

What is List Comprehension?

List comprehension is a way of making list but in a single, short line. Yet, many developers struggle to fully understand list comprehension in Python and use for loops in so many places. That can lead to code that’s less efficient and difficult to read. If you find yourself using a for loop along with .append() to create a list, the list comprehension is a good substitute.

For example, you want to make a list using a for loop using letters from a string. What you may do is the following:

Input:

				
					my_string = 'hello'
my_list = []
for letter in my_string:
    my_list.append(letter)
print(my_list)
				
			

Output:

				
					['h', 'e', 'l', 'l', 'o']
				
			

Now, We are going to do the exact same thing but in one single line! You can check the code yourself. Here it is:

Input:

				
					my_string = 'hello'
my_list = [letter for letter in my_string]
print(my_list)
				
			

Output:

				
					['h', 'e', 'l', 'l', 'o']
				
			

See? We did the same thing. You don’t really need to create an empty list and add each element to the end. You can simply define the list and its contents at the same time by following this form:

				
					a_list = [expression for member in iterable]
				
			

List Comprehension Elements

Every list comprehension in Python includes three elements:

  • The expression : the member itself, a call to a method, or any other actual expression that returns a value. In the example above, letteris a variable but also the simplest form of an expression. You can try a more complex expression. For example, letter * len(my_string). The output would be [‘hhhhh’, ‘eeeee’, ‘lllll’, ‘lllll’, ‘ooooo’]
  • The member is the object or value in the list or any iterable object. In the example above, the member value is letter.
  • The iterable is a list, set, sequence, generator, or any other object that can return its elements one at a time. In the example above, the iterable is my_string which equals the string 'hello'.

Which way is faster? List comprehension or for loop?

You are probably thinking the same thing, “Which way is faster? List comprehension or for loop?”. To find out which way is faster, We did this experiment with Python’s time method (time()) that takes the number of seconds passed since epoch as an argument and returns struct_time in UTC, Coordinated Universal Time. Here is the code (Input):

				
					from time import time
t0 = time()
mylist = []
for i in range(0,1000001):
    mylist.append(f'Seconds passed using for loops: {i*i}')
t1 = time()
print(f'Seconds passed using for loops: {t1 - t0}')
t3 = time()
list2 = [i*i for i in range(0,1000001)]
t4 = time()
print(f'Seconds passed using list comprehension: {t4 - t3}')
				
			

Output:

				
					Seconds passed using for loops: 0.3974893093109131
Seconds passed using list comprehension: 0.09969401359558105
				
			

Did that answer your question? So, as you can see, list comprehension is faster and more efficient or may I say effective than for loops.

2 Comments

  • Suraj kumar
    November 8, 2022 at 1:15 PM Reply

    Bahut aacha hai aap ka

    • Code and Debug
      November 8, 2022 at 1:32 PM Reply

      Thanks for the feedback.

Leave a Reply Cancel reply

About US

At Code & Debug, our mission is to continuously innovate the best ways to train the next generation of developers and to transform the the way tech education is delivered.

Code & Debug was founded in 2020 to bridge the knowledge gap between colleges and industry. Founded by Anirudh Khurana, Code & Debug has professional teaching faculty and a state-of-art learning platform for Coding education.
View Courses

Pages

  • About Us
  • Contact Us
  • Home
  • Offline Courses
  • User Account

Contact Us

  • 1st-29, Atlanta Business Hub, VIP Road, Surat
  • Tel.: +91 97129 28220
  • info@codeanddebug.in