Dictionary & list comprehensions are yet another means of defining dictionaries and lists in a simple manner.
This is example of list comprehension
Code
- list_comp = [i for i in range(4)]
- print(list_comp)
Output:[0, 1, 2, 3] This is example of dictionary
Code
- dictt = {i : i+2 for i in range(10)}
- print(dictt)
Output:
{0: 2, 1: 3, 2: 4, 3: 5, 4: 6, 5: 7, 6: 8, 7: 9, 8: 10, 9: 11}