Python Forum
Topic: “Filter numbers with a list comprehension” (PyBite #107)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Topic: “Filter numbers with a list comprehension” (PyBite #107)
#2
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]]
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Messages In This Thread
RE: Topic: “Filter numbers with a list comprehension” (PyBite #107) - by menator01 - Jun-11-2020, 04:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List comprehension not working right Cris9855 3 1,103 Nov-04-2024, 03:46 PM
Last Post: DeaD_EyE
  Problem with List Comprehension in Python laurawoods 3 1,285 Aug-12-2024, 06:26 AM
Last Post: Pedroski55
  List Comprehension Issue johnywhy 5 2,151 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 21,631 Jan-05-2024, 08:30 PM
Last Post: sgrey
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 1,808 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  find random numbers that are = to the first 2 number of a list. Frankduc 23 7,881 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Using list comprehension with 'yield' in function tester_V 5 3,894 Apr-02-2023, 06:31 PM
Last Post: tester_V
  List of random numbers astral_travel 17 6,156 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 3,956 Nov-13-2022, 01:27 AM
Last Post: menator01
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 3,065 Oct-23-2022, 11:13 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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