loki
loki

Reputation: 972

Unique index values to every element in a python list

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]

Upvotes: 1

Views: 96

Answers (6)

iz_
iz_

Reputation: 16593

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))

Upvotes: 3

Joran Beasley
Joran Beasley

Reputation: 113988

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)

Upvotes: 2

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31993

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)

Upvotes: 1

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

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))]

Upvotes: 1

lehiester
lehiester

Reputation: 900

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)

Upvotes: 0

Alec
Alec

Reputation: 9546

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)

Upvotes: 0

Related Questions