2
finallist = []
for each_time in range(10):
    x = int(input("Whats your number ? "))
    finallist.append(x)

It ask 10 times "Whats your number ? " and .append the obtained answer with input()as int into a list, i wanted to know it there are any simple way of doing this, with map maybe ? i've read on similar problems somethings about json too.

finallist.append(int(input("Whats your number ? ")))

I know i can do this, but for reasons of explanation, i preferred to write the first one.

Bruno Cerk
  • 355
  • 3
  • 16

5 Answers5

5

You could do it with a list comprehension:

finallist = [int(input("Whats your number ? ")) for _ in range(10)]

but I wouldn't recommend doing it, since it's less clear what's going on.

Or, as Zen of Python would say:

>>> import this
...
Explicit is better than implicit.

List comprehensions (and map, filter, reduce, etc.) are best when your function has no side-effects. And asking user for input has side-effects.

randomir
  • 17,989
  • 1
  • 40
  • 55
  • 3
    up-voted for recommending the OP against using this. The OP really isn't gaining that much by using a list comprehension vs a vanilla loop. As @juanpa.arrivillaga said in his comment, premature optimization is very rarely a good idea. – Christian Dean Aug 03 '17 at 19:06
  • It depends on your perspective of whether determining the user's input is a "side effect" or a computation. It isn't "fire the nukes" for i in 1 to 10, it's "determine i'th entered entered number" for i in 1 to 10. – Asad Saeeduddin Aug 03 '17 at 19:13
  • Either way, you're tainting a pure list comprehension with a computation. – randomir Aug 03 '17 at 19:29
  • The point of a list comprehension is to compute a list of things. What do you use list comprehensions for? – Asad Saeeduddin Aug 03 '17 at 19:31
  • I like to use list comprehensions only on pure functions. That way I know I'll always get the same result (list) for the same input -- input being some sequence I'm iterating, not a user. – randomir Aug 03 '17 at 19:38
  • Well, actually i was wondering if my way was slower then other existing ways – Bruno Cerk Aug 03 '17 at 19:56
  • 1
    @BrunoCerk, remember that ["premature optimization is the root of all evil"](https://en.wikiquote.org/wiki/Donald_Knuth#Computer_Programming_as_an_Art_.281974.29). :) Regardless, the slowest part will probably be the user input part, not your loop. – randomir Aug 03 '17 at 20:00
2

You could use a comprehension:

numbers = [int(input("Number please")) for i in range(10)]
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • 2
    Minor improvement: If you never use the variable that you use to iterate over an iterable - `i` in your case - it is common convention to use `_` instead. That let's the reader know that you're only iterating over the iterable for code repetition. – Christian Dean Aug 03 '17 at 18:59
  • @ChristianDean didn't knew that, thanks for the information – Bruno Cerk Aug 03 '17 at 19:41
2

Use comprehension is always a better alternative to a for loop:

finallist = [int(input("Whats your number ? ")) for _ in range(10)]
DYZ
  • 55,249
  • 10
  • 64
  • 93
1
result = []
while not len(result) == 10:
    result += [input('What is your number? ')]
Emerson Cardoso
  • 394
  • 3
  • 5
0

Consider allowing the user to type all of them in a once separated by spaces, something along these lines:

while True:
    s = input("Enter 10 numbers (separated by spaces)? ").split()
    if len(s) != 10:
        print("Sorry, that was {} values, try again".format(len(s)))
    else:
        finallist = [int(n) for n in s]
        break
martineau
  • 119,623
  • 25
  • 170
  • 301