Posts: 63 Threads: 16 Joined: Oct 2018 Nov-03-2018, 04:00 PM (This post was last modified: Nov-03-2018, 04:26 PM by frequency.) How can i multiply even numbers from a list? I have this code credit_card_sum=[] credit_card_int=[[b]5[/b],6,[b]7[/b],8] for i in range(len(credit_card_int)): credit_card_sum.append(credit_card_int[i]) i=i+1 print (credit_card_sum) I want to multiply 5 and 7 by 2 and append them to a list! Posts: 1,150 Threads: 42 Joined: Sep 2016 Nov-03-2018, 04:17 PM (This post was last modified: Nov-03-2018, 04:18 PM by j.crater.) Please put your code in Python code tags, it is required for the code to be displayed in a readable manner. You can find help here. I am not sure I understood you right. Do you want to multiply (by 2) all the integers with an even index in a list? So for your example, the result should be: credit_card_int=[5,6,7,8] credit_card_sum=[10, 14] Is that right? If not please write an example of input and output you want. Posts: 63 Threads: 16 Joined: Oct 2018 Nov-03-2018, 04:29 PM (This post was last modified: Nov-03-2018, 04:30 PM by frequency.) For example credit_card_int=[5,6,7,8] For this list. (happens to be 4 items) i want the even numbers from the i to be multiplied by 2. For example. Since for starts from 0 its cound, 5 and 7 will be multiplied because with the for use 0 and 3 are even numbers. So the 0(first item) and the 3rd item will be multiplied Posts: 440 Threads: 1 Joined: Sep 2018 Nov-03-2018, 04:45 PM (This post was last modified: Nov-03-2018, 04:45 PM by stullis.) Oh! You want the numbers at the even indexes to be doubled. I see it now. Slice the list and then double the numbers with a function or use a list comprehension: credit_card_int=[5,6,7,8] x = [num * 2 for num in credit_card_int[::2]] Posts: 1,150 Threads: 42 Joined: Sep 2016 There are several ways to accomplish this. You could go with: for index, number in enumerate(credit_card_int) if not(index % 2): credit_card_sum.append(number*2) If you don't know how enumerate works, check a reference online, for example here. Also read on modulus operator (%) if you are not familiar with it. Posts: 1,150 Threads: 42 Joined: Sep 2016 Nov-03-2018, 04:50 PM (This post was last modified: Nov-03-2018, 04:51 PM by j.crater.) Suggestion by @ stullis to slice the list is excellent, I wish I had thought of it on my own :) However, as a beginner, I would not go with a list comprehension just yet. Otherwise it's a very nice, and efficient, solution ;) Posts: 63 Threads: 16 Joined: Oct 2018 Nov-03-2018, 09:22 PM (This post was last modified: Nov-03-2018, 09:22 PM by frequency.) Thank you all for your posts! It works very nice. But how can i make it so when it makes the calculations to get the indexed result and remove the number that made the calculation and appanend the new one. So i dont have 2 lists but one. if a list has 3 items. Lets say list=[5,0,2] when the enumerate fuction finishes its job,i must have list=[10,0,4] . Now with this current code if i have list=[5,0,2] i get the result appended to an other list with the following items in it [10,4] . Summarising. How can i make the function to append the result of the calculation to the indexed number,keeping the other unchanged numbers in their position? Thank you! Posts: 440 Threads: 1 Joined: Sep 2018 Using the code @j.crater provided, you can index the original list and change the contents at that index: for index, number in enumerate(credit_card_int) if not(index % 2): credit_card_init[index] = number*2 Posts: 63 Threads: 16 Joined: Oct 2018 Thank you! May i ask how can i split the item from a list that is a 2digit number and make a sum out of those? For example. If item[0] is 15 i must replace 15 with the sum of the 2 digits.So 6! I think i have to make them str ,append them to a list,split them and see whats next Posts: 1,150 Threads: 42 Joined: Sep 2016 Do you mean this? Was the offered solution not alright? |