Skip to content

Instantly share code, notes, and snippets.

@vinaykudari
Created July 21, 2018 21:27
Show Gist options
  • Save vinaykudari/cfa7f0bcd0763a53cda80ab20cc89eb8 to your computer and use it in GitHub Desktop.
Save vinaykudari/cfa7f0bcd0763a53cda80ab20cc89eb8 to your computer and use it in GitHub Desktop.
List Comprehension
#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