1

I have three tuples, listA, listB, and listC.

It is important that in each list, each value keeps the same index as their initial corresponding values in the other two lists.

I have two conditions, one for listA and one for listB. I would like all the values in listA which don't satisfy the listA condition remove, and all the values in listB which don't satisfy the listB conditions removed and so that all the values with the same indices as these removed values in all three lists are also removed.

This will involve removing some values which do satisfy the listA conditions from listA etc if listB condition is not met. So that the result is three lists who's values all indiviudally satisfy their own list's condition (although listC has no condition) and the values have the same pairing as initially.

I have managed to do it by iterating through the list using a for loop, an example of the way that I did this is shown below, the condition imposed on listA is x<10, and for listB is x>20. I'm aware there are much more elegant and pythonic methods for doing this using zip and filtering however I haven't been able to get them to work thus far. I am not a very experienced programmer so any help would be greatly appreciated.

listA=(15,5,2,8,10,11,4,8) 
listB=(19,25,28,14,32,10,21,29)
listC=(0,9,13,4,19,51,83,13)

listAA=[]
listBB=[]
listCC=[]
for i in range(len(listA)):
    if listA[i]<10:
        if listB[i]>20:
            listAA.append(listA[i])
            listBB.append(listB[i])
            listCC.append(listC[i])
        else:
            continue
    else:
        continue
print(listAA,listBB,listCC)
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 2
    It's very good that you've managed to do it using regular loop. It'd be very helpful if you could add your code to the question so that we can help you by starting from that point. Plus, preventing your good question of being closed. – Mazdak Mar 30 '19 at 15:40
  • Thank you for your feedback I've added an example which uses the method I used. – Stanley Walker Mar 30 '19 at 16:01

2 Answers2

0

It is important that in each list, each value keeps the same index as their initial corresponding values in the other two lists.

This suggests that what you really have is eight items, each with three attributes. It would be more convenient to treat each item as an atomic unit. A pythonic approach for that is to model each item as a tuple (as a 3-tuple). So then you would have:

items = [(15, 19, 0),
         (5, 25, 9),
         (2, 28, 13),
         ...,
         (8, 29, 13)]

Now you want a helper function, a predicate, that will tell us whether the item fits some business rules, whether the item is acceptable.

def is_wanted(item):
    """Predicate, true for an item we wish to retain."""
    price, length, age = item  # unpack the tuple
    return (price < 10
            and length > 20)

With that in hand, you're in a position to call filter():

print(list(filter(is_wanted, items)))

If you want to get fancy, you might consider using a named tuple.

J_H
  • 17,926
  • 4
  • 24
  • 44
0

If you have multiple lists that correspond itemwise: zip them together and operate on the zippes - then zip them again to get back to your tuples:

listA = (15,5,2,8,10,11,4,8)           # not a list - it is a tupe
listB = (19,25,28,14,32,10,21,29)      # not a list - it is a tupe
listC = (0,9,13,4,19,51,83,13)         # not a list - it is a tupe

#  create positional tuples, perform tests, reverse tupling, and decompose into tuples    
A,B,C = list(zip(*filter(lambda x:x[0]<10 and x[1]>20, zip(listA,listB,listC))))

print(A)
print(B)
print(C) 

Output:

(5, 2, 4, 8)
(25, 28, 21, 29)
(9, 13, 83, 13)

Doku:


zip( [1,2,3],["a","b","c"],[99,88,77] ) => ((1,"a",99),(2,"b",88),(3,"c",77)) as generator
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69