1

I am trying to multiply only certain number in a list with even index number. My list has some numbers that occur multiple times. These numbers have the same index values. However, I want to multiply those numbers by 2 only if they occur in the place such that their index number is even.

All forums I searched have the opposite of my need, where they want recurring numbers to have the same index number. For my case, I want recurring numbers to have different index values.

a = ['2', '0', '0', '1', '4', '5', '6', '4', '0', '4', '6']
my_list = []
for i in a:
    if a.index(i) in [1,3,5,7,9]: 
        #get ascii value using ord()
        a1 = (ord(i)) * 2
    else:
        a1 = ord(i)
        my_list.append(a1)

Expected = [50, 96, 48, 98, 52, 106, 54, 106, 48, 104, 54]

Got = [50, 52, 54, 52, 52, 54]

6 Answers 6

3

You should use enumerate instead of index. Also, you can make this a list comprehension:

a = ['2', '0', '0', '1', '4', '5', '6', '4', '0', '4', '6']
result = [ord(c) * 2 if i % 2 == 1 else ord(c) for i, c in enumerate(a)]
print(result)
# [50, 96, 48, 98, 52, 106, 54, 104, 48, 104, 54]

Loop equivalent:

result = []
for i, c in enumerate(a):
    if i % 2 == 1:
         result.append(ord(c) * 2)
    else:
         result.append(ord(c))
Sign up to request clarification or add additional context in comments.

Comments

2

You are only appending in your else branch

a = ['2', '0', '0', '1', '4', '5', '6', '4', '0', '4', '6']
my_list = []
for i in a:
    if a.index(i) in [1,3,5,7,9]: 
        #get ascii value using ord()
        a1 = (ord(i)) * 2
    else:
        a1 = ord(i)
    my_list.append(a1)

Comments

1

The other answers got at the fundamental problem (the placement of append()), but if you want to be flashy you can do this in a one-line list comprehension:

a = ['2', '0', '0', '1', '4', '5', '6', '4', '0', '4', '6']
my_list = [ord(a[i])*2 if i % 2 == 1 else ord(a[i]) for i in range(len(a))]

Comments

1

You are missing to add in list for if condition

a = ['2', '0', '0', '1', '4', '5', '6', '4', '0', '4', '6']
my_list = []
for i in a:
    if a.index(i) in [1,3,5,7,9]: 
        #get ascii value using ord()
        a1 = (ord(i)) * 2
        my_list.append(a1)
    else:
        a1 = ord(i)
        my_list.append(a1)

Comments

0

Simply add my_list.append(a1) to your if statement:

a = ['2', '0', '0', '1', '4', '5', '6', '4', '0', '4', '6']
my_list = []
for i in a:
    if a.index(i) in [1,3,5,7,9]: 
        #get ascii value using ord()
        a1 = (ord(i)) * 2
        my_list.append(a1)
    else:
        a1 = ord(i)
        my_list.append(a1)

Or better, just execute it after the else:

a = ['2', '0', '0', '1', '4', '5', '6', '4', '0', '4', '6']
my_list = []
for i in a:
    if a.index(i) in [1,3,5,7,9]: 
        #get ascii value using ord()
        a1 = (ord(i)) * 2
    else:
        a1 = ord(i)
    my_list.append(a1)

Comments

0

There are two main problems here. As others have mentioned, you need to move the append outside of the else branch. You are also discarding the current index of the list element as you loop through it, which you then look up with a.index(i)--this won't give the correct result in the case of repeated elements because it will return the index of the first match. You could e.g. use the enumerate function to get both the index and the element in the loop.

a = ['2', '0', '0', '1', '4', '5', '6', '4', '0', '4', '6']
my_list = []
for idx, i in enumerate(a):
    if idx in [1,3,5,7,9]: 
        #get ascii value using ord()
        a1 = (ord(i)) * 2
    else:
        a1 = ord(i)
    my_list.append(a1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.