List | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

List

How do you search through a list? Example: look if the value of 25 exists in the list: Listc = [40,30,50,25]

4th Feb 2022, 12:59 PM
Lenoname
22 Answers
0
No. Change value in my code, which i posted last,.. And see output. Else part belong to for loop. Not to if block. Lenoname
4th Feb 2022, 3:54 PM
Jayakrishna 🇮🇳
+ 11
# Hi! You use ’in’: print(25 in Listc)
4th Feb 2022, 1:12 PM
Per Bratthammar
Per Bratthammar - avatar
+ 4
Use "in" keyword
4th Feb 2022, 1:39 PM
Abu Habban
Abu Habban - avatar
+ 2
n = 30 # Is 30 in list? mylist = [10, 20, 40, 30, 50] for val in mylist: if val == n: print(n, "found in list!") break else: print(n, "not found in list.")
4th Feb 2022, 3:10 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Can i do it with a loop?
4th Feb 2022, 2:31 PM
Lenoname
+ 1
Jayakrishna🇮🇳 Because the chapter i’m at still hasn’t introduced ”in” yet.
4th Feb 2022, 2:49 PM
Lenoname
+ 1
Yes, I already, updated on that doubt. See code again.. change input and try again code.
4th Feb 2022, 3:34 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 like this?i changed b on purpose, gives no output b = 70 a = [5,7,8,4,9] for i in a: if i == b: print("Score") break print("0 points")
4th Feb 2022, 3:41 PM
Lenoname
+ 1
listc=[40,30,50,25] Print(listc[3])
5th Feb 2022, 3:31 PM
Shyam Prasad
+ 1
if any(list(map(lambda x: True if x==25 else False, Listc))): print(True )
6th Feb 2022, 11:18 AM
hajar ait addi
hajar ait addi - avatar
0
Yes. You can Try.. But if if done in a single line, then why need loop!!!
4th Feb 2022, 2:41 PM
Jayakrishna 🇮🇳
0
Then go for loop. Internally ' in ' does the same, I think.
4th Feb 2022, 2:51 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 almost zero idea how
4th Feb 2022, 2:57 PM
Lenoname
0
Lenoname You have now code already.., Learn more by completing lessons.. There you can find details with examples...
4th Feb 2022, 3:17 PM
Jayakrishna 🇮🇳
0
Per Bratthammar I just want the ”score” to be printed out, not the rest. b = 4 a = [5,7,8,4,9] for i in a: if i == b: print("Score") break else: print("0 points")
4th Feb 2022, 3:24 PM
Lenoname
0
Says 0 point 0 point 0 point Score
4th Feb 2022, 3:25 PM
Lenoname
0
Remove else part. or Lenoname python loop have advantage, to use else with it, executed only when no matching inside.. b = 4 a = [5,7,8,4,9] for i in a: if i == b: print("Score") break else: print("0 points")
4th Feb 2022, 3:27 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 and if it doesnt find 25 in the list?(lets say its not there) i want it to say ”0 point”
4th Feb 2022, 3:32 PM
Lenoname
0
Hola
5th Feb 2022, 2:22 PM
Jhonayfer Eduardo Catalan Moneris
Jhonayfer Eduardo Catalan Moneris  - avatar