+ 2

what is list comprehension in Python?

Can i use if else statement in that. Explain me in simple words.

20th Aug 2018, 8:32 AM
Nitin Gutte
Nitin Gutte - avatar
3 Answers
+ 3
List comprehensions provide a concise way to create lists. It consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The expressions can be anything, meaning you can put in all kinds of objects in lists. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. The list comprehension always returns a result list. The basic syntax is = [ expression for item in list if conditional ] e.g x=[i for i in range(10) if i%2==0] print(x) #output=[0,2,4,6,8]
20th Aug 2018, 11:09 AM
Maninder $ingh
Maninder $ingh - avatar
0
#you can; #Example styles=["bold", "underlined", "italic", "colourful"] style=input("Enter a style: ") print(style) if(style in styles): print("You entered a style") else: print("You didnt enter a style")
20th Aug 2018, 10:30 AM
Sousou
Sousou - avatar