Python: How would one represent this particular for loop using list comprehension? -
as title states, know how use "list comprehension" shorten loop. loop functional; however, details of assignment given me states should have @ least 1 list comprehension , loop in code. way, new python.
z=0 elements in if a[z] in c: z=z+1 elif a[z] in b: c.append(a[z]) z=z+1 else: z=z+1
also, if there general tips shorten this, appreciated.
i assume want take elements a
, place them in c
if elements present in b
. want make sure 1 such element exits in c
, meaning c
set.
you can as
>>> = [1, 3, 5, 7, 3] >>> b = [3, 5] >>> set([ in if in b ]) set([3, 5])