Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List conversion - inplace

I am trying to convert ['1','2','3','4'] to [1,2,3,4] I want to make this conversion inplace. Is it possible to do it? If not, what is the optimal solution.

like image 908
Aman Avatar asked Jun 23 '26 16:06

Aman


2 Answers

I think it is better to use map for this kind of tasks. Which creates iterator, what means it is more memory efficient.

l = list(map(int, l))
# here I convert it to list, but usually you would just iterate over it
# so you can just do
for item in map(int, l):
    ...
like image 105
Sardorbek Imomaliev Avatar answered Jun 26 '26 04:06

Sardorbek Imomaliev


you can do it with list comprehension like this:

l = [int(item) for item in l]
like image 24
yinwoods Avatar answered Jun 26 '26 04:06

yinwoods



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!