How can I find out if there is even, or odd, number of elements in an arbitrary list.
I tried list.index()
to get all of the indices... but I still don\'t k
You can use the built in function len()
for this.
Python Doc -- len()
Gets the length (# of elements) of any arbitrary list.
myList = [0,1,2,3,4,5]
if len(myList) % 2 == 0:
print ("even")
else
print ("odd")
Define function that returns a bool (true or false).
def is_even(myList):
if len(myList) % 2 == 0:
return true
else:
return false
main():
myList = [0,1,2,3]
theListIsEven = is_even(myList) # will be true in this example
# because 4 items in myList
if theListIsEven(myList) == True:
# do something
else:
# do something else
return 0
The modulus operator %
gives the remainder.
EX: 7 % 2 = 1
EX: 4 % 2 = 0
def has_even_length(some_sequence):
return not len(some_sequence)%2
def has_odd_length(some_sequence):
return bool(len(some_sequence)%2)
your_list = [1,2,3,(4,5)]
# modulo operation finds the remainder of division of one number by another.
if len(your_list) % 2 == 0:
print "Even Number"
else:
print"number is odd"
Even numbers are divisible by 2. Odd numbers are not.
len(X)
will get the length of X
If the length of X
is divisible by 2, then it is an Even number
If the length of X
is not divisible by 2 then it is an Odd Number
len(X)%2
returns the "remainder" of a division problem
for example 5%2
will return 1
which is NOT zero, (because 5 divided by 2 is 2 with a remainder of 1), therefore it is not even.
Same thing as 6%4
which would return a 2
, because 6 divided by 4 is 1 with a remainder of 2.
so len(X)%2
where X
is your list, will return either a 1, indicating it is Odd, or a 0 indicating it is Even.
if you want to be bitwise about it you could also use.
if len(mylist)&1:
print("odd")
else:
print("even")
this is really Nice looking if you know your list is more likely to be odd.
if len(mylist)&1:
print("list was odd")
elif CheckSomthingElse():
print("list was even AND CheckSomthingElse was True")
else:
print("list was even AND CheckSomthingElse was False")
Wow in python by the test below % 2 is faster. python must be optimized for it. in some other languages & 1 is faster. this is why it's important to always test.
import timeit
MyListOdd=[1,2,3,4,5,6,7]
MyListEven=[1,2,3,4,5,6]
print("MyListOdd == ",MyListOdd)
print("MyListEven == ",MyListEven)
print("len(MyListOdd) == ",len(MyListOdd))
print("len(MyListEven) == ",len(MyListEven))
def TestIfEvenBitwise(MyList):
if len(MyList)&1:
return False #Odd
else:
return True #Even
def TestIfEvenModulus(MyList):
if len(MyList)%2:
return False #Odd
else:
return True #Even
print("TestIfEvenBitwise(MyListOdd) == ",TestIfEvenBitwise(MyListOdd))
print("TestIfEvenModulus(MyListOdd) == ",TestIfEvenModulus(MyListOdd))
print("TestIfEvenBitwise(MyListEven) == ",TestIfEvenBitwise(MyListEven))
print("TestIfEvenModulus(MyListEven) == ",TestIfEvenModulus(MyListEven))
mysetup = """
MyListOdd=[1,2,3,4,5,6,7]
MyListEven=[1,2,3,4,5,6]
def TestIfEvenBitwise(MyList):
if len(MyList)&1:
return False #Odd
else:
return True #Even
def TestIfEvenModulus(MyList):
if len(MyList)%2:
return False #Odd
else:
return True #Even
"""
print("timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListOdd)', number=100000) == ",
timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListOdd)', number=100000))
print("timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListOdd)', number=100000) == ",
timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListOdd)', number=100000))
print("timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListEven)', number=100000) == ",
timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListEven)', number=100000))
print("timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListEven)', number=100000) == ",
timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListEven)', number=100000))
results of test.
MyListOdd == [1, 2, 3, 4, 5, 6, 7]
MyListEven == [1, 2, 3, 4, 5, 6]
len(MyListOdd) == 7
len(MyListEven) == 6
TestIfEvenBitwise(MyListOdd) == False
TestIfEvenModulus(MyListOdd) == False
TestIfEvenBitwise(MyListEven) == True
TestIfEvenModulus(MyListEven) == True
timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListOdd)', number=100000) == 0.02574796500000004
timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListOdd)', number=100000) == 0.022446242000000005
timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListEven)', number=100000) == 0.026081517000000054
timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListEven)', number=100000) == 0.025758655999999935
All you need is
len(listName)
Which will give you the length.
I guess you could also do this then
if len(listName) % 2 == 0:
return True # the number is even!
else:
return False # the number is odd!