What will be the value of ‘result’ in following Python program? list1 = [1,2,3,4] list2 = [2,4,5,6] list3 = [2,6,7,8] result = list() result.extend(i for i in list1 if i not in (list2+list3) and i not in result) result.extend(i for i in list2 if i not in (list1+list3) and i not in result) result.extend(i for i in list3 if i not in (list1+list2) and i not in result)
What will be the value of ‘result’ in following Python program?
list1 = [1,2,3,4] list2 = [2,4,5,6] list3 = [2,6,7,8] result = list() result.extend(i for i in list1 if i not in (list2+list3) and i not in result) result.extend(i for i in list2 if i not in (list1+list3) and i not in result) result.extend(i for i in list3 if i not in (list1+list2) and i not in result)
a. [1, 3, 5, 7, 8]
b. [1, 7, 8]
c. [1, 2, 4, 7, 8]
d. error
1 Answers
a. [1, 3, 5, 7, 8]
Explanation: Here, ‘result’ is a list which is extending three times. When first time ‘extend’ function is called for ‘result’, the inner code generates a generator object, which is further used in ‘extend’ function. This generator object contains the values which are in ‘list1’ only (not in ‘list2’ and ‘list3’). Same is happening in second and third call of ‘extend’ function in these generator object contains values only in ‘list2’ and ‘list3’ respectively. So, ‘result’ variable will contain elements which are only in one list (not more than 1 list).