3
\$\begingroup\$
tablou = input("Enter values delimited by space: ")
b = tablou.split()

t = [] # initial  list with int() contents
for l in b:
    r = int(l)
    t.append(r)

I have to do multiple assignments which require working with lists of integers/floats. Above is what I'm using for converting a string to a proper list of integers. I was wondering if there's any way to optimize the code in a such way that it won't need to use memory to keep tablou, b and the final list. I would be actually working with t

I'm not sure if I used the right terms to describe the issue, I'm still learning.

\$\endgroup\$
6
\$\begingroup\$

Releasing memory

If you assign None to your variables, their contents is lost and the memory will (eventually) be reclaimed.

tablou = input("Enter values delimited by space: ")

b = tablou.split()
tablou = None

t = [] # initial  list with int() contents

for l in b:
    r = int(l)
    t.append(r)
b = None
l = None
r = None

Forgetting the variables

The variables tablou, l, b, and r will still exist; they will just contain the value None. If you accidentally use the variable tablou again, you won't get a NameError: name 'tablou' is not defined, although you may get an error stating the operation cannot be performed on the value None.

Better would be to delete the variables when they are no longer needed:

del tablou
del l, b, r

After deleting the variables, it is an error to refer to them again, without first storing a value under that name.

List Comprehension

Repeatedly appending values to a list is an expensive operation. It is usually faster to create the list using "list comprehension".

First, note that instead of this ...

t = []

for l in b:
    r = int(l)
    t.append(r)

... you could write this, eliminating the r variable:

t = []

for l in b:
    t.append(int(l))

Then, you can replace the list creation, loop, append with the following:

t = [ int(l) for l in b ]

This does the same thing as the former code, but without the repeated append call. As a bonus, the list comprehension variable, l does not escape into the local scope; there is no need to del l.

Mapping

Applying the same operation to every element of a list is called a "mapping", and Python comes with a built in function map() to do just that.

In your specific case, you are calling int( ) on every item in the list b, so instead of:

t = [ int(l) for l in b ]

you could write:

t = list(map(int, b))

Advance topic: The map() function actually returns an iterator, rather than a list. In many cases, you can avoid the call to list(), which iterates over all the items the iterator can produce and builds a list of out them, and simply use the returned iterator directly.

One-liner

You can reduce your code to one line, removing all temporary variables, using nested calls:

t = list(map(int, input("Enter values delimited by space: ").split()))

I don't recommend it, however. It does not result in easy-to-understand code. It is better to separate "user input" from "processing".

Variable Names

t, r, l and b are horrible variable names. Name the variables with longer, descriptive names.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.