1

I want to use a list of indices to remove items from my list of tuples:

mytupList = [(1,2),(2,3),(5,6),(8,9)]
indxList = [1,3]

I have tried using numpy like so:

newtupList = numpy.delete(mytupList,indxList).tolist()

but it has not worked.

I want my newtupList = [(1,2),(5,6)]

what am I doing wrong? I have also tried:

a = np.array(mytupList) 
newtup = np.delete((a),indxList)

but this does not produce the desired result.

wunderkind
  • 169
  • 1
  • 8

2 Answers2

1

As mentioned in the docs, you need to use the axis option there, because without it being mentioned it would delete elements on a flattened version. Thus, you need to do like this -

np.delete(mytupList,indxList,axis=0).tolist()

Sample run -

In [21]: mytupList
Out[21]: [(1, 2), (2, 3), (5, 6), (8, 9)]

In [22]: indxList
Out[22]: [1, 3]

In [23]: np.delete(mytupList,indxList).tolist() # Flattens and deletes
Out[23]: [1, 2, 5, 6, 8, 9]

In [24]: np.delete(mytupList,indxList,axis=0).tolist() # Correct usage
Out[24]: [[1, 2], [5, 6]]

To retain the format of list of tuples, use map after deleting, like so -

map(tuple,np.delete(mytupList,indxList,axis=0))

Sample run -

In [16]: map(tuple,np.delete(mytupList,indxList,axis=0))
Out[16]: [(1, 2), (5, 6)]
Divakar
  • 218,885
  • 19
  • 262
  • 358
0

My answer in this post: Deleting multiple elements from a list

for i in sorted(indices, reverse=True):
    del somelist[i]

See that this does remove the items you want instead of making a new list a replace the original variable with the new list. (I haven't tested which on is more efficient, I would expect this one if the list is large enough)

Community
  • 1
  • 1
tglaria
  • 5,678
  • 2
  • 13
  • 17
  • mylist = [(23,23),(434,353),(353,3535),(1425,475)] – wunderkind Jan 14 '16 at 13:01
  • oops...yes that does work as well (tested) although I was convinced that I could this on a list of tuples. More efficient as in faster? – wunderkind Jan 14 '16 at 13:08
  • I did not get your question. Do this on a list of tuples? Didn't work? And yes, efficient as 'more faster'. – tglaria Jan 14 '16 at 13:48
  • ok so I generated a list of tuples which are coordinate points and found some code to create polylines from pair of the pairs of tuples. I have to do more reading about lists, tuples and list comprehensions as it seems I did not understand that a list containing tuples can be treated as a normal list. So I became fixated on find a solution to deal with tuples and used the solution suggested by @Divakar and discounted yours. However yours does work thanks. – wunderkind Jan 17 '16 at 05:54
  • UPDATE - @tglaria I've tested your solution on a small list of tuples and it works but when I try it on a larger dataset (1000s of tuples) I get what had feared would happen: Traceback (most recent call last): File "O:\mytests\coords.py", line 71, in del pairt[i] TypeError: 'tuple' object doesn't support item deletion. Divakar's solution doesn't produce this tuple issue – wunderkind Jan 17 '16 at 08:09
  • @wunderkind this would happen when you're trying to delete an item from a tuple. Are you sure your data is EXACTLY a list of tuples? This solution lets you delete any item from a LIST, no matter its contents. It doesn't matter if it's a list of tuples, strings or whatever. Could you paste your data somewhere to check your problem? – tglaria Jan 18 '16 at 11:50