This workspace is provided for your convenience. You can use this activecode window to try out anything you like.
[<expression> for <item> in <sequence> if <condition>]
forclause iterates through each item in a sequence. The items are filtered by the
ifclause if there is one. In the example above, the
itemtake on all the values in the list
mylist. Each item is then squared before it is added to the list that is being built. The result is a list of squares of the values in
primes_uptofunction we will use the
is_primefunction to filter the sequence of integers coming from the
rangefunction. In other words, for every integer from 2 up to but not including
n, if the integer is prime, keep it in the list.
def primes_upto(n): """ Return a list of all prime numbers less than n using a list comprehension. """ result = [num for num in range(2,n) if is_prime(num)] return result
alist = [4,2,8,6,5] blist = [num*2 for num in alist if num%2==1] print(blist)