I'm supposed to be able to convert a given number into a range of numbers that will be in a list (Example: given the number 10, list will contain 1 2 3 4 5 6 7 8 9 10), then, from that list it's supposed to delete all the even numbers, and print both the list with all the numbers and the list without all the even numbers. I've tried many useless things without getting a proper idea on how to make it. (Tried many tutorials on lists and python forums)
-
2Could you show us the code you've tried so far?Chuck– Chuck2016-04-07 04:45:01 +00:00Commented Apr 7, 2016 at 4:45
-
limite = int(raw_input("Give me a limit: ")) lim_lista = range(limite) print lim_listaIsaac Lo– Isaac Lo2016-04-07 04:46:44 +00:00Commented Apr 7, 2016 at 4:46
Add a comment
|
4 Answers
given_number = 10
l = list(range(1, given_number + 1))
odds = [i for i in l if i%2]
print(l, odds)
2 Comments
Isaac Lo
Seems fairly easy. I'd just have to replace "Given_number = 10" to the user input. Thanks!
Sergey Gornostaev
@IsaacLo you can mark the answer as a solution for thanks ;)
Assuming Python 3:
user_input = input("Please give the number: ")
lst = [i+1 for i in range(0, user_input)]
odd_lst = [i for i in lst if i%2]
2 Comments
Ozgur Vatansever
[i+1 for i in range(0, user_input)] is kind of an overkill. Do list(range(1, given_number + 1)) instead.Chuck
How so? I thought it would be a good alternative solution. (I'm fairly new to using list comprehensions, is
list() better than using comprehensions?)