Generating a list of EVEN numbers in Python
Solution 1
Use a list comprehension (see: Searching a list of objects in Python)
myList = [<your list>]
evensList = [x for x in myList if x % 2 == 0]
This is good because it leaves list intact, and you can work with evensList as a normal list object.
Hope this helps!
Solution 2
The following sample should solve your problem.
Newlist = []
for x in numList:
if x % 2 == 0:
print x
Newlist.append(x)
Solution 3
You can do this with a list comprehension:
evens = [n for n in numbers if n % 2 == 0]
You can also use the filter
function.
evens = filter(lambda x: x % 2 == 0,numbers)
If the list is very long it may be desirable to create something to iterate over the list rather than create a copy of half of it using ifilter
from itertools
:
from itertools import ifilter
evens = ifilter(lambda x: x % 2 == 0,numbers)
Or by using a generator expression:
evens = (n for n in numbers if n % 2 == 0)
Solution 4
In your specific case my_list[1::3]
will work. There are always two odd integers between even integers in fibonacci: even, odd, odd, even, odd, odd.....
>>> my_list = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
>>>
...
>>> my_list[1::3]
[2, 8, 34, 144, 610, 2584, 10946, 46368]
Solution 5
Just check this
A = [i for i in range(101)]
B = [x for x in A if x%2 == 0]
print B
Admin
Updated on September 26, 2020Comments
-
Admin over 3 years
Basically I need help in generating even numbers from a list that I have created in Python:
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, ...]
I have tried a couple different methods, but every time I print, there are odd numbers mixed in with the evens!
I know how to generate even/odd numbers if I were to do a range of 0-100, however, getting only the even numbers from the previous mentioned list has me stumped!
P.S. I've only been using python for a couple days, if this turns out to be extremely simple, thanks in advance!
EDIT: Thanks for all the replies, with your help I've gotten through this little problem. Here is what I ended up with to complete a little excercise asking to sum the even numbers of fibonacci sequence:
F = [1, 2] while F[-1] < 4000000 F.append(F[-1] + F[-2]) sum(F[1::3]) 4613732
-
jathanism almost 12 yearsThis case works, but because you're relying on slicing and stepping by index it's not a portable solution.
-
Trufa almost 12 yearsI think list comprehension might be a little to advanced for the first days with python... but that's just my opinion!
-
Erty Seidohl almost 12 yearsI see you're slicing the list, but there's two colons? What is this method called?
-
Colin Dunklau almost 12 years@Trufa list comprehensions are one of the best parts about python... why not introduce them early?
-
Erty Seidohl almost 12 yearsOh I agree, but this is the best practice - and it leaves him with a list object.
-
mgilson almost 12 years@Erty -- The third number is the "stride". You start at the first element and then take every 3rd element after that.
-
Erty Seidohl almost 12 yearsThis is if you want to print all the even numbers - if you want to get a list that you can work with, see the answers below. Edit: I see the new version appends it to a new list :)
-
Trufa almost 12 yearsOP, this is a great answer but consider that lambda is a little too advanced for the first days of python :)
-
Katriel almost 12 yearsUgh,
ifilter
? Just(x for x in numbers if not x % 2)
works. -
Trufa almost 12 yearsI just think it would be healthier to learn the for loops ins and outs before getting into list comprehension. But again, IMHO.
-
David Webb almost 12 yearsHave added a generator expression, although I went for
n % 2 == 0
-
Ryan Haining almost 12 yearsnah, list comprehensions are way better for this kind of thing
-
barbiepylon almost 12 yearsagreed...I've never seen them before. They look fast and efficient!
-
jonrsharpe almost 10 yearsIt would be nice if you included some information regarding what this does. Also, you could save a lot of code using the third
step
argument torange
. -
bohnpessatti about 2 yearsFor a cleaner code, you could simply use
if not x%2
instead ofif x % 2 == 0