Python - standard Flashcards
Create a list comprehension consisting of squares of the elements from another list
new_list = [ i**2 for i in old_list ]
Create an equivalent list comprehension for the following:
new_list = []
for letter in ‘abcd’:
for num in range(4):
new_list.append( (letter, num) )
new_list = [ (letter, num) for letter in ‘abcd’ for num in range(4) ]
Create a list comprehension that filterers another list for only odd numbers
new_list = [ i for i in old_list if i%2 != 0 ]
Create an equivalent list comprehension for the following:
names= ['Bruce', 'Clark', 'Peter'] heroes= ['Batman', 'Superman', 'Spiderman']
new_dict= {}
for name, hero in zip(names, heroes):
new_dict[name] = hero
new_dict = { name: hero for name, hero in zip( names, heroes) }
Create a lambda function that cubes an input value
lambda x: x **3
Convert the following into one line of code
if x > y:
print(‘x is bigger’)
else:
x*y
print(‘x is bigger’) if x>y else x*y
Get a list of the keys from a dictionary
dict.keys()
Get a list of tuples of key-value pairs from a dictionary
dict.items()
Get a list of the values from a dictionary
dict.values()