Josh
Josh

Reputation: 314

looping through the items of a list in Python

A simple problem, I am trying to loop through each element of this list, the list itself is formed as a list of lists..

Price = [500, 300, 100, 200]
list_of_pairs = [[1, 1], [2, 2], [1, 1], [2, 2]]

p_11 = []
p_22 = []
p_33 = []

for i in list_of_pairs:
    if i[0] == 1 and i[1] == 1:
        p_11.append(Price[list_of_pairs.index(i)])

    elif i[0] == 2 and i[1] == 2:
        p_22.append(Price[list_of_pairs.index(i)])

    elif i[0] == 3 and i[1] == 3:
        p_33.append(Price[list_of_pairs.index(i)])


print(list_of_pairs)

print(p_11)
print(p_22)

The problem is when I loop through the list and try to classify each element(sublist) by it's first and second value, It looks like this:

#the output:
[[1, 1], [2, 2], [1, 1], [2, 2]]
[500, 500]
[300, 300]

#what I expected(wanted) for the output:
[[1, 1], [2, 2], [1, 1], [2, 2]]
[500, 100]
[300, 200]

Upvotes: 0

Views: 61

Answers (3)

pascscha
pascscha

Reputation: 1673

You could use enumerate instead of list_of_pairs.index to get the index of the current element:

for j, i in enumerate(list_of_pairs):
    if i[0] == 1 and i[1] == 1:
        p_11.append(Price[j])

    elif i[0] == 2 and i[1] == 2:
        p_22.append(Price[j])

    elif i[0] == 3 and i[1] == 3:
        p_33.append(Price[j])

This is however not really a nice solution, perhaps a better way of doing the same thing could be like this:

price = [500, 300, 100, 200]
list_of_pairs = [(1, 1), (2, 2), (1, 1), (2, 2)]

# Dictionary that stores the prices for all pairs
p = {}

# Iterate over all prices and pairs at the same time
for price, pair in zip(price, list_of_pairs):
    if pair not in p:
        p[pair] = [price]
    else:
        p[pair].append(price)

print(p[(1,1)]) # [500, 100]
print(p[(2,2)]) # [300, 200]

Upvotes: 2

marcos
marcos

Reputation: 4510

Because index returns the index of the first value in the list that matches that, since you have duplicates, you get wrong values, use enumerate to get the index:

Price = [500, 300, 100, 200]
list_of_pairs = [[1, 1], [2, 2], [1, 1], [2, 2]]

p_11 = []
p_22 = []
p_33 = []

for i, (a,b) in enumerate(list_of_pairs):
    if a == 1 and b == 1:
        p_11.append(Price[i])

    elif a == 2 and b == 2:
        p_22.append(Price[i])

    elif a == 3 and b == 3:
        p_33.append(Price[i])


print(list_of_pairs)
>>> [[1, 1], [2, 2], [1, 1], [2, 2]]

print(p_11)
>>> [500, 100]

print(p_22)
>>> [300, 200]

Upvotes: 0

Nikos M.
Nikos M.

Reputation: 8325

Try using enumerate to get the index directly it is easier and faster:

Price = [500, 300, 100, 200]
list_of_pairs = [[1, 1], [2, 2], [1, 1], [2, 2]]

p_11 = []
p_22 = []
p_33 = []

for index,i in enumerate(list_of_pairs):
    if i[0] == 1 and i[1] == 1:
        p_11.append(Price[index])

    elif i[0] == 2 and i[1] == 2:
        p_22.append(Price[index])

    elif i[0] == 3 and i[1] == 3:
        p_33.append(Price[index])


print(list_of_pairs)

print(p_11)
print(p_22)

Upvotes: 1

Related Questions