Python Forum

Full Version: Need help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I want to read some numbers into my list and i want to insert them in 1 line:
1 2 3 4...
instead of:
1
2
3
4
When I read it in 1 line, it treats the numbers as a text, to change the format from text to number, I have to do it one by one. Is there any way to speed up the process and change the whole list into a number format?
t.extend(input().split())
for i in range(n):
    t[i]=int(t[i]) 
Something like this.
>>> lst = ['1', '2', '3' ,'4']
>>> lst = [int(i) for i in lst]
>>> lst
[1, 2, 3, 4]
>>> 
>>> for i in lst:
...     print(i, end=' ') 
...     
1 2 3 4 
inp_str = input("Enter numbers ")
inp_list = eval(inp_str)  # 1, 2, 3, 4
# or
inp_list = list(map(int, inp_str.split()))  # 1 2 3 4
Thanks snippsat, I used your code, unfortunately the amount of steps the program executes stays the same.
Tanks deanhystad, but I don't really know how to implement your code in my situation.
(Nov-26-2020, 08:43 PM)Radez Wrote: [ -> ]Tanks deanhystad, but I don't really know how to implement your code in my situation.
t = eval(input()) # Type numbers separated by commas: 1, 2, 3, 4
# or
t = list(map(int, input())  # Type numbers separated by spaces: 1 2 3 4
Should not advice to use eval() with input() @deanhystad.
It's a security risk,old input input() in Python 2 had this problem(did use eval).
Was changed in Python 3,so it return only a string then can use it with int(input(' ')) or float(input(' ')) if that's needed.

Example this could be my input string.
So if named a real exciting file it would be removed.
inp_str = '__import__("os").remove("important_file")'
eval(inp_str)
Error:
FileNotFoundError Traceback (most recent call last) 1 inp_str = '__import__("os").remove("important_file")' 2 eval(inp_str) FileNotFoundError: [Errno 2] No such file or directory: 'important_file'
More on the subject of eval() and how futile is trying to mitigate risks associated with it: Eval really is dangerous
I tried a few times to use your code but it still uses the same amount of steps to change text to numbers as my old one did.
for i in range(n):
    t[i]=int(t[i]
(Nov-27-2020, 10:18 AM)Radez Wrote: [ -> ]I tried a few times to use your code but it still uses the same amount of steps to change text to numbers as my old one did.
for i in range(n):
    t[i]=int(t[i]

The number of steps is the same because you have to process every number entered, but... lookup of items at specific indices takes time (you make two lookups for every iteration) and this code is not idiomatic Python. It is something you write in Java. In Python you can iterate directly over items:

for item in n:
    # do something with item
There is list comprehension (example provided earlier by snippsat) for creating new list from iterable. As snippsat pointed out, you can do: 'split user input on whitespaces and convert every item in resulting list to integer'. This is efficient and idiomatic Python.