Question #59005

Given that k refers to an int and that play_list has been defined to be a list, write a expression that evaluates to True if the value associated with k is an element of play_list.

Expert's answer

Question #59005, Programming & Computer Science / Python

Task: Given that k refers to an int and that play_list has been defined to be a list, write a expression that evaluates to True if the value associated with k is an element of play_list.

Answer: Let's define variables


>> k = 5
>> play_list = [1,2,3,4,5]


The easiest way to check if value is in lsit is to use keyword in:


>> k in play_list
True


Next we can create wrapper for it:


>> f = lambda v, l: True if v in l else False
>> f(k, play_list)
True


We can also create wrapper using def instead of lambda:


>> def f(k, l):
...     if k in l:
...         return True
...     else:
...         return False
>> f(k, play_list)
True


http://www.AssignmentExpert.com/

LATEST TUTORIALS
APPROVED BY CLIENTS