Python Forum
determine if an number is in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
determine if an number is in a list
#1
what should i use for the elif statement to get number not in the list

import random

def  rollBall():

    system = [0,9,20,24,25,35]

    spin = random.randint(0,36)
    if spin in system:
        return True
    elif spin ??? system:
        return False
Reply
#2
import random
 
def rollBall():
    system = [0,9,20,24,25,35]
    spin = random.randint(0,36)
    return spin in system
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
You really don't need the elif. You could use a else instead because the initial logic statement is binary: the number either is or is not in the list. Since there are no other possibilities, you only require the one test at the beginning. If that test fails, then the number is evidently not in the list.

With that out of the way, you could use @buran's answer or you could use the keyword "not":

import random
 
def  rollBall():
 
    system = [0,9,20,24,25,35]
 
    spin = random.randint(0,36)
    if spin in system:
        return True
    elif spin not in system:
        return False
Reply
#4
No elif is necessary, as you return True if found and code exits there
import random
 
def rollBall():
 
    system = [0,9,20,24,25,35]
 
    spin = random.randint(0,36)
    if spin in system:
        return True

    ## if no return above then num not in system
    return False 
Reply
#5
The return False is useless because the function returns None if the if condition isn't met. None evaluates to False.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Nov-06-2018, 04:06 AM)wavic Wrote: The return False is useless because the function returns None if the if condition isn't met. None evaluates to False.
Although it's true that None is evaluated to False your suggestion has at least 2 weak points - (i) you don't know how the value returned from function is used later on in the code and there might be undesired side-effects when return None instead of False; and (ii) Explicit is better than implicit.
I would always prefer my original suggestion, but for completeness, one more possible code snippet is

if spin in system:
    return True
else:
    return False
i.e. there is no need for second evaluation.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
You can also use
return bool(spin in system)
This makes it explicit that the return value is True or False, although this is probably already the case with the in operator.
Reply
#8
(Nov-06-2018, 08:51 AM)Gribouillis Wrote: You can also use
return bool(spin in system)
This makes it explicit that the return value is True or False, although this is probably already the case with the in operator.
spin in system is already a bool, no need to cast it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,012 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,755 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,195 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 7,924 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 2,980 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  Determine number of all the immediately adjacent points in python zackk 1 1,823 Feb-06-2021, 09:23 AM
Last Post: zackk
  Count number of occurrences of list items in list of tuples t4keheart 1 2,342 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 1,927 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 1,897 Jul-24-2020, 01:46 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020