Created
July 21, 2018 21:27
-
-
Save vinaykudari/cfa7f0bcd0763a53cda80ab20cc89eb8 to your computer and use it in GitHub Desktop.
List Comprehension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#example_1 | |
numbers = [1, 2, 3, 4] | |
squares = [n**2 for n in numbers] | |
>> squares | |
[1, 4, 9, 16] | |
#example_2 | |
obj = ["Even" if i%2==0 else "Odd" for i in range(5)] | |
>> obj | |
['Even', 'Odd', 'Even', 'Odd', 'Even'] | |
#example_3 | |
matrix = [[1, 2],[3,4],[5,6],[7,8]] | |
transpose = [[row[i] for row in matrix] for i in range(2)] | |
>> transpose | |
[[1, 3, 5, 7], [2, 4, 6, 8]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment