Skip to content

Instantly share code, notes, and snippets.

@akaomy
Created November 12, 2018 02:00
Show Gist options
  • Save akaomy/acba318c2fb84e9ade9b370096c76c8b to your computer and use it in GitHub Desktop.
Save akaomy/acba318c2fb84e9ade9b370096c76c8b to your computer and use it in GitHub Desktop.
nums = [1,2,3,4,5,6,7,8,9,10]
my_list = []
for n in nums:
my_list.append(n)
print(my_list)
=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list = [n for n in nums]
print(my_list)
=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
###
my_list = []
for n in nums:
my_list.append(n*n)
print(my_list)
=>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
my_list = [n*n for n in nums]
print(my_list)
=>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# same for lambda function but it's not so readable as list comprehension
my_list = map(lambda n: n*n, nums)
print(my_list)
###
# I want n for each n in nums if n is even
my_list = []
for n in nums:
if n%2 == 0:
my_list.append(n)
print(my_list)
=>[2, 4, 6, 8, 10]
my_list = [n for n in nums if n%2 == 0]
print(my_list)
=>[2, 4, 6, 8, 10]
# same for lambda function but it's not so readable as list comprehension
my_list = filter(lambda n: n%2 == 0, nums)
###
# I want a (letter, num) pair for each letter in 'abcd' and each number in '0123'
my_list = []
for letter in 'abcd':
for num in range(4):
my_list.append((letter, num))
print(my_list)
=>[('a', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 0), ('b', 1), ('b', 2), ('b', 3), ('c', 0), ('c', 1), ('c', 2), ('c', 3), ('d', 0), ('d', 1), ('d', 2), ('d', 3)]
my_list = [(letter, num) for letter in 'abcd' for num in range(4)]
print(my_list)
=>[('a', 0), ('a', 1), ('a', 2), ('a', 3), ('b', 0), ('b', 1), ('b', 2), ('b', 3), ('c', 0), ('c', 1), ('c', 2), ('c', 3), ('d', 0), ('d', 1), ('d', 2), ('d', 3)]
###
# Dictionary comprehensions
names = ['Bruce', 'Clark', 'Peter', 'Logan']
herps = ['Batman', 'Superman', 'Spiderman', 'Wolverine']
print(zip(names, heros))
=>{'Bruce': 'Batman', 'Clark': 'Superman', 'Peter': 'Spiderman', 'Logan': 'Wolverine'}
my_dict = {}
for name, hero in zip(names, heros):
my_dict[name] = hero
print(my_dict)
=>{'Bruce': 'Batman', 'Clark': 'Superman', 'Peter': 'Spiderman', 'Logan': 'Wolverine'}
my_dict = {name: hero for name, hero in zip(names, heros)}
print(my_dict)
=>{'Bruce': 'Batman', 'Clark': 'Superman', 'Peter': 'Spiderman', 'Logan': 'Wolverine'}
my_dict = {name: hero for name, hero in zip(names, heros) if name != 'Peter'}
print(my_dict)
=>{'Bruce': 'Batman', 'Clark': 'Superman', 'Logan': 'Wolverine'}
# Set comprehensions - unordered collection of the unique values
# more about Sets > https://realpython.com/python-sets/
nums = [1,1,2,1,3,4,5,5,6,7,7,6,8,6,8,9,9]
my_set = set()
for n in nums:
my_set.add(n)
print(my_set)
=>{1, 2, 3, 4, 5, 6, 7, 8, 9}
my_set = {n for n in nums}
my_set
=>{1, 2, 3, 4, 5, 6, 7, 8, 9}
###
# Generator expression: 17:19 -... https://www.youtube.com/watch?v=3dt4OGnU5sM
# What does the 'yield' keyword do?
# https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment