How to convert the output of a for loop into a list in Python 3?

For loop output as dictionary in python
Fig: For Loop Output as Dictionary in Python

Example 1


>>> mylist = []
>>> for y in range(0,10):
    mylist.append(y)
>>> mylist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 2


>>> [x for x in range(0,10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 3


>>> dic = []
>>> for char in 'Binary Study: Programming Made Easy':
dic.append(char)

>>> print(dic)
['B', 'i', 'n', 'a', 'r', 'y', ' ', 'S', 't', 'u', 'd', 'y', ':', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', ' ', 'M', 'a', 'd', 'e', ' ', 'E', 'a', 's', 'y']


Advertisements


Comments