top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Writer's picturebismark boateng

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.


syntax:

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"]
print(h_letters)

When we run the program, the output will be:


['l','e','t','t','e','r']

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


0 comments

Recent Posts

See All

Comments


bottom of page