1

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)

2
  • 2
    Could you show us the code you've tried so far? Commented Apr 7, 2016 at 4:45
  • limite = int(raw_input("Give me a limit: ")) lim_lista = range(limite) print lim_lista Commented Apr 7, 2016 at 4:46

4 Answers 4

4
given_number = 10
l = list(range(1, given_number + 1))
odds = [i for i in l if i%2]
print(l, odds)
Sign up to request clarification or add additional context in comments.

2 Comments

Seems fairly easy. I'd just have to replace "Given_number = 10" to the user input. Thanks!
@IsaacLo you can mark the answer as a solution for thanks ;)
1

range has a third parameter, step.

For odd:

range(1, number + 1, 2)

For even:

range(2, number + 1, 2)

Comments

0

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

[i+1 for i in range(0, user_input)] is kind of an overkill. Do list(range(1, given_number + 1)) instead.
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?)
-1
myRange = 10
myList = list(xrange(myRange))

evenList = [x for x in myList if x % 2 == 0]
oddList = [x for x in myList if x % 2 != 0]

print myList
print evenList
print oddList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.