-2

In order to put the input into a list:

  numbersList = [int(n) for n in input('Enter numbers: ').split()]

Can someone explain what does 'int(n) for n in' mean?

How do I improve this question?

Isabel
  • 51
  • 7
  • 1
    What the code is doing is that, it takes integers as numbers from the user input and the .split() function converts it to an list – Mazhar Khan Oct 28 '19 at 15:12
  • 1
    minor clarification - the line of code takes an input from the user, splits it into n individual pieces (assuming those are numbers), turns each number represented as string into a number represented as integer, and puts all those number in a list, using a list comprehension (that's what the outer brackets do). – MrFuppes Oct 28 '19 at 15:16
  • See this: [What does “list comprehension” mean? How does it work and how can I use it?](https://stackoverflow.com/questions/34835951/what-does-list-comprehension-mean-how-does-it-work-and-how-can-i-use-it) – Georgy Oct 28 '19 at 15:29
  • maybe also insteresting, why you should aim to use comprehensions if possible (not only because it looks nice...): https://stackoverflow.com/questions/30245397/why-is-a-list-comprehension-so-much-faster-than-appending-to-a-list – MrFuppes Oct 28 '19 at 15:59
  • thank you everyone! your answers are so comprehensive! thanks! @MrFuppes – Isabel Oct 28 '19 at 16:16
  • Does this answer your question? [converting str to int in list comprehension](https://stackoverflow.com/questions/51745416/converting-str-to-int-in-list-comprehension) – Joe May 23 '20 at 10:13

3 Answers3

3

The entire expression is referred to as a List Comprehension. It's a simpler, Pythonic approach to construct a for loop that iterates through a list.

https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

Given your code:

numbersList = [int(n) for n in input('Enter numbers: ').split()]

Lets say you run the code provided, you get a prompt for input:

Enter numbers: 10 8 25 33

Now what happens is, Python's input() function returns a string, as documented here:

https://docs.python.org/3/library/functions.html#input

So the code has now essentially become this:

numbersList = [int(n) for n in "10 8 25 33".split()]

Now the split() function returns an array of elements from a string delimited by a given character, as strings.

https://www.pythonforbeginners.com/dictionary/python-split

So now your code becomes:

numbersList = [int(n) for n in ["10", "8", "25", "33"]]

This code is now the equivalent of:

numbersAsStringsList = ["10", "8", "25", "33"]
numberList = []
for n in numbersAsStringsList:
    numberList.append(int(n))

The int(n) method converts the argument n from a string to an int and returns the int.

https://docs.python.org/3/library/functions.html#int

Kronosfear
  • 69
  • 4
2

For example input('Enter numbers: ').split() returns an array of strings like ['1', '4', '5']

int(n) for n in will loop throug the array and turn each n into an integer while n will be the respective item of the array.

J. Sadi
  • 2,718
  • 1
  • 13
  • 26
1

let us try to understand this list comprehension expression though a simple piece of code which means the same thing.

nums = input('Enter numbers: ') # suppose 5 1 3 6 

nums = nums.split() # it turns it to ['5', '1', '3', '6']

numbersList = [] # this is list in which the expression is written

for n in nums: # this will iterate in the nums.
    number = int(n) # number will be converted from '5' to 5
    numbersList.append(number) # add it to the list

print(numbersList) # [5, 1, 3, 6]
P.hunter
  • 1,203
  • 2
  • 15
  • 39