-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListComprehension.py
More file actions
41 lines (29 loc) · 1.07 KB
/
Copy pathListComprehension.py
File metadata and controls
41 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
"""ListComprehension.py: Demo code for List Comprehension"""
__author__ = "Sumit Kala"
'''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 ]
'''
def ListCreator():
mylist = range(10)
yield mylist
mylist2 = [x ** 2 for x in range(5)] # list of squares from 0-4
yield mylist2
mylist3 = [(x, x * 2) for x in range(10)] # list of tuples
yield mylist3
def main():
x = ListCreator()
# print 100 to 70
for i in x.__next__():
print(f'{i}', end=" ") # print in same line
print(f'')
for i in x.__next__():
print(f'{i}', end=" ") # print in same line
print(f'')
for i in x.__next__():
print(f'{i}', end=" ") # print in same line
if __name__ == '__main__':
main()