0

How to check if multiple integers are in a list ?

How to check if 1 or 2 or 3 is in a list [4,5,2,6] and return true if the list contains at least 1 or 2 or 3

11th Jul 2020, 4:25 PM
David Nierman
David Nierman - avatar
8 Réponses
+ 3
You can do it by just iterating through the list and check of required condition for every element of the list. Or You can make an hash map of the list if you want the check for a lot of values.
11th Jul 2020, 4:27 PM
Arsenic
Arsenic - avatar
+ 2
Use the or keyword multiple times in you condition Like if ( 1 in list or 2 in list or 3 in list ): To understand it in a better way Here's the code . https://code.sololearn.com/c5QjZHQVDBnc/?ref=app I hope it will help you. Thank you.
11th Jul 2020, 4:32 PM
Akshay Panwar
Akshay Panwar - avatar
+ 1
Arsenic this example is small, but I'd like to check for 10 plus numbers and keep my code clean & not repeated. How would you go about making the hashmap and hashmap same a dict in python ?
11th Jul 2020, 4:36 PM
David Nierman
David Nierman - avatar
+ 1
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 thank you! Nice and concise! How did you learn you could use a for loop like this that allows you to define a variable from one list and iterate through list b to determine if that var exists ? I've spent a day searching for documentation on the (i in a for i in b) but cannot find anything to learn more.. just keeps bringing me to normal for loops i.e. for x in b:
12th Jul 2020, 3:53 PM
David Nierman
David Nierman - avatar
11th Jul 2020, 4:48 PM
David Nierman
David Nierman - avatar
0
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 check if [21,3,4,88,9,2,1,25] is in [1,54,87,45,65,34,3,6,9,47,21,23,31,64] This would return true because 1 from the first list in the second list
11th Jul 2020, 6:16 PM
David Nierman
David Nierman - avatar