Generating a list of EVEN numbers in Python

前端 未结 14 620
轻奢々
轻奢々 2021-01-17 15:48

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         


        
14条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 16:16

    Use a list comprehension (see: Searching a list of objects in Python)

    myList = []
    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!

提交回复
热议问题