4

I need to be able to multiply every second number in a list by 2 so say:

List = [1,2,3,4]

I want this to return [1,4,3,8] but all the ways that I have tried it such as

credit_card = [int(x) for x in input().split()]

credit_card[::2] = [x*2 for x in credit_card[::2]]

print(credit_card)

If i input the same list from before it returns [2,2,6,4]

Is there a way to accomplish what I'm trying to accomplish?

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
Ethan O.
  • 43
  • 1
  • 5
  • Possible duplicate of [How do I loop through a Python list by twos?](http://stackoverflow.com/questions/2990121/how-do-i-loop-through-a-python-list-by-twos) – SierraOscar Feb 10 '16 at 14:16
  • 1
    Just a handy tip for you: Avoid naming your variables using built-in names such as `list`, `dict`, `id`, etc.. (here you used big `L` so its not such a problem, but a variables `should_look_like_this` and `ClassesAreWrittenLikeThis`. Makes code more readable and saves you headaches down the road – redacted Feb 10 '16 at 14:31

6 Answers6

4

You are almost there, you just need to start from the second (1-indexed) element:

credit_card[1::2] = [x*2 for x in credit_card[1::2]]

That said, since you seem to be implementing the Lunh checksum, you only need a sum of those digits without having to update the original data, like done in this example.

bereal
  • 32,519
  • 6
  • 58
  • 104
  • 1
    Just a short explanation for OP: the general syntax is `credit_card[start:stop:step]`. `[::2]` would mean "Take all elements from start to stop with step of 2 and `[2::2]` would mean "start at 3rd element, go to the end, with step of 2 – redacted Feb 10 '16 at 14:27
1
lst = [1,2,3,4]

new_lst = [2*n if i%2 else n for i,n in enumerate(lst)]     # => [1, 4, 3, 8]
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0
credit_card = input().split()
for x in len(credit_card)
    if x % 2 != 0
        credit_card[x] = credit_card[x] * 2

print (credit_card)
trog
  • 1
  • 1
  • 2
0

Another solution using enumerate:

[i* 2 if p % 2 else i for p, i in enumerate(l)]

where i is the p-th element of l.

sopor
  • 46
  • 7
  • Good answer. Just change `item` to `i` (now, it won't work) :) – Nander Speerstra Feb 10 '16 at 14:28
  • @spoor, @Nander Speerstra: what is the purpose of the `%` operator? –  Feb 10 '16 at 15:09
  • @Jon. You basically check if the position is even or odd by checking if the remainder of the division by 2 is 0 (even) or not (odd). – sopor Feb 10 '16 at 15:13
  • @sopor: so the reason you can say `if p % 2` instead of `if p % 2 == 0` is because Python takes the 0 value as a truthy value and 1 as a falsy value... right? Meaning that 0 passes the `if` and any other value fails it –  Feb 10 '16 at 15:28
  • 1
    @Jon, now: if the index is **odd**, use **p*2** (**else**: **p**). If you say `if p % 2 == 0`, you would have to **reverse** the `if/else`: `i if p % 2 == 0 else i*2`. Now you say: if the index is **even**, print **p** (**else**: use **p*2**). It's a matter of what you like to be in the `if` and the `else`... – Nander Speerstra Feb 10 '16 at 16:16
  • @NanderSpeerstra: Right -- got that backwards. What I meant to ask was whether `if p % 2 == 1` is equivalent to `if p/2 == 1` ... or in other words, what does the `%` operator do? –  Feb 10 '16 at 16:25
  • No: `8/2==1` will return `False`, and `7/2==1` as well (in fact, only `p=2` will give True). But `8%2` is 0, and `7%2` is 1. – Nander Speerstra Feb 10 '16 at 16:31
  • Simply put, the modulo operator ([other explanation](http://stackoverflow.com/questions/2664301/how-does-modulus-divison-work)) says for `p%x`: extract x from p as long as possible. So 8%2 => 8, then 6, then 4, then 0. Done. And 7%2 => 7, then 5, then 3, then 1 and done (since 1-2 will return a negative number). – Nander Speerstra Feb 10 '16 at 16:34
  • @NanderSpeerstra: Ah, I get it! Thanks –  Feb 10 '16 at 17:06
0
for i,_ in enumerate(credit_card):
    if i%2:
        credit_card[i] *= 2

or if you want to be fancy:

 credit_card=[credit_card[i]*(2**(i%2)) for i in range(len(credit_card))]
NickZ
  • 101
  • 7
0
>>> l = [1,2,3,4]
>>> 
>>> list(map(lambda x: x*2 if l.index(x)%2 else x, l))
[1, 4, 3, 8]
Iron Fist
  • 10,739
  • 2
  • 18
  • 34