I want to multiply each element in my list by 2, but I got ['123123', '456456', '789789'] instead of [246, 912, 1578].
Here's my code
list = ['123', '456', '789']
my_new_list = []
for i in list:
my_new_list.append(i*2)
print (my_new_list)
what should i change or add to the code to get [246, 912, 1578]?
You are multiplying strings. Instead multiply integers.
list = ['123', '456', '789']
my_new_list = []
for i in list:
my_new_list.append(int(i)*2)
print (my_new_list)
Or just make every number in list an integer. Also here is a list comprehension version of your code
list = ['123', '456', '789']
my_new_list = [int(i)*2 for i in list]
List Comprehension you should look into it. What does "list comprehension" mean? How does it work and how can I use it?