(Jan-13-2020, 11:22 AM)Armin Wrote: I understand split nouw but what i can't understand is this part of the code in line 7
“y = [i for i in input “
this part of the code and why this code is there because the program works without this part of the code if we put in line 7 above, for example
y = input('give me a number: ').split(',')
we get the same result. I understand that this is the work of codo o comprehensive list but not understood why it's there"i in i for input " in lijn 7.
Thanks in advance
This list comprehension is already described by Clunk_Head as overkill (and I agree with that assessment). There is no need for that, you can just split the input on comma and get the same result. As why it is written this way - you should consult the author.
Regarding list comprehension: usage of it could be justified if string is converted into float thus avoiding converting it later:
>>> y = [i for i in input('give me a number: ').split(',')] # original
give me a number: 4,2
>>> y
['4', '2']
>>> y = input('give me a number: ').split(',') # code provided by Clunk_Head with same result
give me a number: 4,2
>>> y
['4', '2']
>>> y = [float(i) for i in input('give me a number: ').split(',')] # with conversion to float
give me a number: 4,2
>>> y
[4.0, 2.0]
If the task is to iterate over the numbers only once then the generator expression could be used.