top of page

Data Scientist Program


Free Online Data Science Training for Complete Beginners.

No prior coding knowledge required!

An introduction to List Comprehension

python is eminent for encouraging developers and Data Scientist write efficient, easy to understand code and a simple to read code as well

List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.


newList =[ expression(element) for element in oldList if condition ]

Advantages of List Comprehension

  1. Require fewer lines of code

  2. Transforms iterative statement into a formula

  3. More time efficient and space efficient than loops

Iterating through a String using List comprehensions

h_letters = [letter for letter in "letter"]

When we run the program, the output will be:


in the above example, a new list is assigned to variable h_letters, and list contains the items of the iterable string 'letters'

we type the print() function to call the output


Recent Posts

See All


bottom of page