One of the nice feature of Python is that it let you create new list from existing list in simple one liners in pythonic way. Python 3 also introduces set and dictionary comprehensions. Also we can use if literal to filter some of the items.
>>> fruits = ['Banana', 'Apple', 'Lime']
>>> loud_fruits = [fruit.upper() for fruit in fruits]
>>> print(loud_fruits)
['BANANA', 'APPLE', 'LIME']
>>> print [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print [i for i in range(20) if i%2 == 0]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
No comments :
Post a Comment