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

3
  • clusters_order_ids.values is a list of lists, not numbers. Commented Jan 14, 2016 at 16:02
  • Use any() to just check if the element exists. any(bulk_order in id_list for id_list in clusters_order_ids) Commented Jan 14, 2016 at 16:20
  • Is your array a numpy array or something else? Commented Jan 14, 2016 at 17:24

2 Answers 2

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']]
Sign up to request clarification or add additional context in comments.

7 Comments

It doesn't delete either. neither entire list containing 523 nor 523
Works for remove_order_with_id = '523' but not for remove_order_with_id = bulk_order
Could it be that you need to access the order_id from the bulk order: remove_order_with_id = bulk_order.order_id?
bulk_order is a list.
Is bulk_order a list of order_ids, also containing the number 3? In that case, what should happen if the array contains 3?
|
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;

3 Comments

Use any() to just check if the element exists. any(bulk_order in id_list for id_list in clusters_order_ids)
@tglaria It gives me output as False
@user2927983 What gives you output as False?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.