7

I have 2 lists of numbers, one called xVar and the other called yVar. I will use these 2 elements to plot X and Y values on a graph. They both have the same number of elements.

Normally, I would just plot

ax.scatter(xVar,yVar,s=2,color='tomato');

I want to remove data from yVar that are over a certain value, for example all data that have a yVar value over 100, but I also want to remove the associated xVar value. Can somebody suggest a way to create 2 new variables that remove all values in yVar over 100 and the xVar values associated with them? Then I can just substitute xVar & yVar in my plotting line to the new variables.

Thanks again All,

iruvar
  • 22,736
  • 7
  • 53
  • 82
bigCow
  • 193
  • 2
  • 5
  • 12

3 Answers3

15

Whenever you want to do something to the corresponding values of two (or more) lists, that's what zip is for. It gives you one list, of the corresponding values for each index.

So, in this case, zip the two lists together, then filtered the zipped list, then unzip them (with zip again, as the documentation explains):

xVar, yVar = zip(*((x, y) for x, y in zip(xVar, yVar) if y <= 100))

If this is confusing, let me show it step by step:

>>> xVar = [1, 200, 300, 10]
>>> yVar = [150, 100, 50, 200]
>>> xyVar = zip(xVar, yVar)
>>> xyVar
[(1, 150), (200, 100), (300, 50), (10, 500)]
>>> xyFiltered = [(x, y) for x, y in xyVar if y <= 100]
>>> xyFiltered
[(200, 100), (300, 50)]
>>> xVar, yVar = zip(*xyFiltered)
[(200, 300), (100, 50)]
abarnert
  • 354,177
  • 51
  • 601
  • 671
1

Without writing the code, find the index for the y you want to remove, save the index, remove the y value, then remove the x value at the same index number.

Dylan Lawrence
  • 1,503
  • 10
  • 32
  • 1
    But there be tricks/gremlins when removing multiple elements (e.g. when removal is during iteration of the list) this way .. – user2246674 Sep 26 '13 at 01:07
  • @user2246674: Just one trick, really: You have to reverse-sort the indices before removing them. But yeah, it's really easy to forget to do that, or get it wrong. – abarnert Sep 26 '13 at 01:10
0

Find indices which correspond to data you want to retain:

ind = [i for i in range(len(yVar)) if yVar[i] < 100]

And use only those points:

yVar = [yVar[i] for i in ind]
xVar = [xVar[i] for i in ind]
sashkello
  • 17,306
  • 24
  • 81
  • 109
  • 2
    you might want to check out [enumerate](http://docs.python.org/2/library/functions.html#enumerate) – iruvar Sep 26 '13 at 01:10