0

For these 2 lists:

A=['3.40', '4.00', '151.00', '8.00', '81.00', '23.00', '17.00', '8.50', '5.00', '151.00', 'SCR', 'SCR', '13.00']
B=['11', '5', '2', '4', '6', '9', '7', '8', '10', '1', '12', '10', '3']

The desired output is:

C=['11', '5', '2', '4', '6', '9', '7', '8', '10', '1', '3']

So - list 'A' and list 'B' are the same length. List 'C' is the same as list 'B' - but does not have the values where 'SCR' exists in list 'A'.

My attempt at this is:

C = [x for x in B if x in A!='SCR']

Thankyou

New Dev
  • 48,427
  • 12
  • 87
  • 129
GDog
  • 163
  • 9

3 Answers3

4

just zip them together:

C = [b for a,b in zip(A,B) if a != 'SCR']
GalSuchetzky
  • 785
  • 5
  • 21
1

Based on what I think you're trying to accomplish, I think you would need this:

C = [B[x] for x in range(len(B)) if A[x] != 'SCR']
Llama Boy
  • 103
  • 8
  • 1
    May be worth reading: [Never use "for i in range(len(sequence))](https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence) and [Loop like a native](https://nedbatchelder.com/text/iter.html) – jarmod Sep 13 '20 at 19:20
1

This is straightforward using the built-in enumerate function:

[x for (idx, x) in enumerate(B) if A[idx] == 'SCR']
Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34