##### Topic: “Filter numbers with a list comprehension” (PyBite #107)
 Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four Splitter Posts: 158 Threads: 63 Joined: Nov 2017 Reputation: 5 Jun-11-2020, 03:55 PM Here is the task I am working on: Write a script which receives a list of numbers and returns only the even numbers that are > 0 and even (divisible by 2). Use list comprehension. Here was my final valiant attempt: ``` numbers = list(range(-10, 11)) # numbers = [2, 4, 51, 44, 47, 10] # numbers = [0, -1, -3, -5] result = [] result = [result.append(num) for num in numbers if num % 2 == 0 or num <= 0] print(result)```For lines 1-3, you can comment/uncomment them to test different list inputs. I was close but slightly off. The problem with my script is that the `result` appends `None` for every list item generated. The reason why this is wrong is because `result` when originally defined is empty (None Type) and for every iteration through the comprehensive list, it just keeps adding None. The solution that the online coursewhere presents is similar to mine (very close) but instead of using the `append` class method, it’s just `result`. Here is the one line solution: `result = [n for n in numbers if n > 0 and n % 2 == 0]`. My basic, simple pointed question for all of you is this: Do all list comprehensions automatically somehow know to append list items automatically? Are the append/extend class methods implied or assumed? This is my effort to solve PyBite #107. Reply menator01 Minister of Silly Walks Posts: 359 Threads: 53 Joined: Sep 2019 Reputation: 21 Jun-11-2020, 04:09 PM Not really sure what you are after but for the "None", this works. ```numbers = list(range(-10, 11)) # numbers = [2, 4, 51, 44, 47, 10] # numbers = [0, -1, -3, -5] result = [] result.append([num for num in numbers if num % 2 == 0 or num <= 0]) print(result)`````Output:[[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 2, 4, 6, 8, 10]]`` Reply bowlofred Giant Foot Posts: 968 Threads: 3 Joined: Mar 2020 Reputation: 119 Jun-11-2020, 05:14 PM (Jun-11-2020, 03:55 PM)Drone4four Wrote: Do all list comprehensions automatically somehow know to append list items automatically? Sorta, yes. They are list comprehensions, so they return a list of the elements. You don't have to create it by manually appending anything. Likewise a dict comprehension will populate a dict, and a set comprehension will append to a set. ```r = range(-5, 6) l = [x for x in r if x % 2 == 0] # all the even numbers. l is created here, no appending to other list. print(l)`````Output:[-4, -2, 0, 2, 4]``In your attempt you are appending the entire list generated into an empty list. So you just get an extra level of nesting. Reply knackwurstbagel Silly Frenchman Posts: 26 Threads: 0 Joined: Dec 2017 Reputation: 2 Jun-11-2020, 08:18 PM (This post was last modified: Jun-11-2020, 08:19 PM by knackwurstbagel.) I think bowlofred did an excellent job answering this so there really is not much for me to add. However, I wanted to say that when I first was introduced to comprehension they confused me as well. Until I realized they are just a fancy way of writing a for loop. For example `result = [n for n in numbers if n > 0 and n % 2 == 0]` could be rewritten as ```result = list() for n in numbers: if n > 0 and n % 2 == 0: result.append(n) print(result)```One thing that I think should be mentioned is. You are creating a list of numbers in a range with `numbers = list(range(-10, 11))` which is perfectly valid and does work. However, note that a comprehension just like a for loop can iterate over a sequence. Range returns an sequence type so you could simplify this with `result = [n for n in range(-10, 11) if n > 0 and n % 2 == 0]` Reply Drone4four Splitter Posts: 158 Threads: 63 Joined: Nov 2017 Reputation: 5 Jun-11-2020, 08:31 PM I actually initially jumped into this exercise writing a regular for loop and came up with something slightly different (but created the same result) to what you came up with, : ```def filtering(numbers): for num in numbers: if num <= 0: continue if num % 2 == 0: list_of_numbers.append(num) else: continue return list_of_numbers```Mine is a little less elegant, but after I came up with that, I then proceeded to achieve the same result with list comprehension (or almost). Sharing alternate script solutions with someone else helps. So thank you, , , and Reply

 Possibly Related Threads… Thread Author Replies Views Last Post adding numbers in a list Nickd12 2 75 7 hours ago Last Post: Serafim What is the difference between a generator and a list comprehension? Pedroski55 2 125 Jan-02-2021, 04:24 AM Last Post: Pedroski55 For Loop with List Comprehension muzikman 25 414 Dec-18-2020, 10:45 PM Last Post: muzikman Sorting list of names using a lambda (PyBite #5) Drone4four 2 360 Oct-16-2020, 07:30 PM Last Post: ndc85430 Using recursion instead of for loops / list comprehension Drone4four 4 338 Oct-10-2020, 05:53 AM Last Post: ndc85430 Uses cases for list comprehension (encountered while doing PyBite #77) Drone4four 3 439 Sep-25-2020, 12:14 PM Last Post: ndc85430 Print max numbers in a list jimmoriarty 1 257 Sep-25-2020, 07:29 AM Last Post: DPaul Using my REPL to bisect numbers and lists with classes (PyBite #181) Drone4four 2 302 Sep-24-2020, 01:47 PM Last Post: Drone4four importing a list of numbers into python script barrypyth 8 603 Aug-22-2020, 09:10 PM Last Post: barrypyth dilemma with list comprehension spalisetty06 1 300 Aug-14-2020, 09:18 AM Last Post: buran

Forum Jump:

### User Panel Messages

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