List | Sololearn: Learn to code for FREE!

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]

2/4/2022 12:59:04 PM

Lenoname

22 Answers

New Answer

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

+11

# Hi! You use ’in’: print(25 in Listc)

+5

https://code.sololearn.com/cAcU5UN813SW/?ref=app

+4

Use "in" keyword

+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.")

+1

Can i do it with a loop?

+1

Jayakrishna🇮🇳 Because the chapter i’m at still hasn’t introduced ”in” yet.

+1

Yes, I already, updated on that doubt. See code again.. change input and try again code.

+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")

+1

listc=[40,30,50,25] Print(listc[3])

+1

if any(list(map(lambda x: True if x==25 else False, Listc))): print(True )

0

Yes. You can Try.. But if if done in a single line, then why need loop!!!

0

Then go for loop. Internally ' in ' does the same, I think.

0

Jayakrishna🇮🇳 almost zero idea how

0

Lenoname You have now code already.., Learn more by completing lessons.. There you can find details with examples...

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")

0

Says 0 point 0 point 0 point Score

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")

0

Jayakrishna🇮🇳 and if it doesnt find 25 in the list?(lets say its not there) i want it to say ”0 point”

0