+ 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..?
5 Respostas
+ 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
+ 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
+ 1
because [3,4] is considerered as a single element of the list
+ 1
how to get "yes" there with in operator..??
+ 1
num=[1,2,[3,4]]
if 3 in num[2]:
print('yes')
else:
print('no')
o/p..?