简体   繁体   中英

printing a single value of a list with enumerate function

I have the following statement:

for i, char in enumerate(list(range(100))):

now i just want to print the value, where char is equal to 50.

I have tried the following idea, but did not understand why it is incorrect:

for i, char in enumerate(list(range(100))):

  char == 50

  print(i)

The solution is that I have to put an "if" statement, i already understood the logic behind it but also want to ask, why my approach is not valid.

Thanks for helping in advance.

You're almost there, you just have to use the if condition like so:

for i, char in enumerate(list(range(100))):
    if char == 50:   #<--- add if here
        print(i)     #<--- inside the if-condition

Also, I see there is no need to use enumerate or list .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM