list question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

list question

num=[1,2,[3,4]] if 3 in num: print('yes') else: print('no') and i expexted yes but the answer is no explain..?

13th Jul 2017, 2:32 PM
sai kumar
sai kumar - avatar
5 Answers
+ 5
Here num contains 3 elements - 1, 2, [3,4]. 3 is part of a list within the num list, not nums directly, so you have to search the sub list to find 3 (: >>> nums = [1,2,[3,4]] >>> 3 in nums False >>> 3 in nums[2] True
13th Jul 2017, 2:44 PM
Maya
Maya - avatar
+ 3
because 3 is inside inner list. What interpretes sees is: 1 2 list [3,4] you'd have to access inner list to get "yes" output
13th Jul 2017, 2:37 PM
Jakub Stasiak
Jakub Stasiak - avatar
+ 1
because [3,4] is considerered as a single element of the list
13th Jul 2017, 2:39 PM
Simone Gramegna
Simone Gramegna - avatar
+ 1
how to get "yes" there with in operator..??
13th Jul 2017, 2:40 PM
sai kumar
sai kumar - avatar
+ 1
num=[1,2,[3,4]] if 3 in num[2]: print('yes') else: print('no') o/p..?
13th Jul 2017, 2:41 PM
sai kumar
sai kumar - avatar