1

I have a list

bulk_order
Out[230]: 
[3    523
Name: order_id, dtype: object]

And I have an array. Its a series but I am accessing it with values

clusters_order_ids.values
Out[231]: 
array([['520', '521', '524', '527', '532'], ['528'], ['531'],
   ['525', '526', '533'], ['519', '523', '529', '534', '535'], ['530']],     
dtype=object)

Now I want to check whether list element 523 is present in above array and if it is there I want to delete it.

I am doing following in python

bulk_order in clusters_order_ids.values

But It gives me False output

Neil
  • 7,937
  • 22
  • 87
  • 145

2 Answers2

3

Try this (from Making a flat list out of list of lists in Python):

l = clusters_order_ids.values
out = [item for sublist in l for item in sublist]
print (bulk_order in out)

For the deletion, you'd have to enter on each list so:

for sublist in clusters_order_ids.values:
    if bulk_order in sublist:
        sublist.remove(bulk_order)
        if not sublist:
            #do something to remove the empty list
        break;
Community
  • 1
  • 1
tglaria
  • 5,678
  • 2
  • 13
  • 17
3

Your list is not a list, but a list of lists.

If you want to delete the whole list containing something in list ['523']:

orders = [['520', '521', '524', '527', '532'], ['528'], ['531'], ['525', '526', '533'], ['519', '523', '529', '534', '535'], ['530']]
remove_order_with_ids = ['523'] # or bulk_order 
orders = [order for order in orders if not set(remove_order_with_ids).intersection(set(order))]
print orders
# [['520', '521', '524', '527', '532'], ['528'], ['531'], ['525', '526', '533'], ['530']]

If you only want to delete items in ['523'] from the inner list(s):

orders = [['520', '521', '524', '527', '532'], ['528'], ['531'], ['525', '526', '533'], ['519', '523', '529', '534', '535'], ['530']]
remove_order_with_id = ['523'] # or bulk_order 
new_orders = []
for order in orders:
    new_orders.append([item for item in order if item not in remove_order_with_id])
print new_orders
# [['520', '521', '524', '527', '532'], ['528'], ['531'], ['525', '526', '533'], ['519', '529', '534', '535'], ['530']]
stoffen
  • 570
  • 6
  • 14
  • It doesn't delete either. neither entire list containing `523` nor `523` – Neil Jan 14 '16 at 16:35
  • Works for `remove_order_with_id = '523'` but not for `remove_order_with_id = bulk_order` – Neil Jan 14 '16 at 16:43
  • Could it be that you need to access the order_id from the bulk order: remove_order_with_id = bulk_order.order_id? – stoffen Jan 14 '16 at 17:06
  • bulk_order is a list. – Neil Jan 14 '16 at 17:13
  • Is bulk_order a list of order_ids, also containing the number 3? In that case, what should happen if the array contains 3? – stoffen Jan 14 '16 at 17:16
  • 3 is an index,not a list element – Neil Jan 14 '16 at 17:20
  • thanks alot. was hunting for it for so long. I used your solution and @tglaria solution combined.. `out1=[item for sublist in bulk_order for item in sublist]` and then I assigned this to `remove_order_with_id=out1` – Neil Jan 14 '16 at 17:43