0

I was trying to append few elements to a list list_accepted_outsidenestant. When i try to print the list list_accepted_outsidenestant, i get: list_accepted_outsidenestant- [([971, 977, 728, 740], set([728, 977, 971, 740]))]. The list is showing a list and set with same elements. Can anyone pointout the mistake i am doing? Because of this, i am getting an error: set_accepted_outsidenest_antlist = set(list_accepted_outsidenestant TypeError: unhashable type: 'list'

I have shown part of code only relevant to the current question.

def leo(tag_data):
 available_ants_outside = []
 ori = []
 for id, (x, y) in tag_data:
 available_ants_outside.append(id)
 if for_coordinates_outside_nest((x, y)) is True:
 ori.append(id)
 return ori
def virgo(tag_data):
 available_ants_inside = []
 list_insidenest_ant_id = []
 set_inside_nest_ant_id = set()
 for id, (x, y) in tag_data:
 available_ants_inside.append(id)
 if for_coordinates_inside_nest((x, y)) is True:
 list_insidenest_ant_id.append(id)
 set_inside_nest_ant_id = set(list_insidenest_ant_id)
 return list_insidenest_ant_id,set_inside_nest_ant_id
 def bambino(ori,list_insidenest_ant_id):
 list_accepted_outsidenestant = [] 
 set_accepted_outsidenest_antlist = set()
 set_accepted_insidenest_antlist = set()
 if len(list_accepted_outsidenestant) < num_accepted:
 if (len(ori) > 0) or (len(list_insidenest_ant_id) >0):
 list_accepted_outsidenestant.extend(ori[0:min(len(ori), 
 num_accepted-len(list_accepted_outsidenestant))])
 set_accepted_outsidenest_antlist = set(list_accepted_outsidenestant) 
 print "list_accepted_outsidenestant-" + str(list_accepted_outsidenestant)
 set_accepted_insidenest_antlist = set(list_insidenest_ant_id)
 return set_accepted_outsidenest_antlist,set_list_outsideant_id,set_accepted_insidenest_antlist
asked Aug 30, 2017 at 10:24
5
  • 2
    This code is very hard to read. Please try and trace down where the problem is introduced and the behavior is different from what you expect. Commented Aug 30, 2017 at 10:33
  • In other words, please create a minimal reproducible example to demonstrate your problem. Commented Aug 30, 2017 at 10:49
  • @ChristianDean What i want is list_accepted_outsidenestant = [971, 977, 728, 740] Commented Aug 30, 2017 at 10:53
  • @philippd . I am just trying to extend the elements to a list list_accepted_outsidenestant. What i expect is; list_accepted_outsidenestant = [971, 977, 728, 740]. What i am getting is list_accepted_outsidenestant- [([971, 977, 728, 740], set([728, 977, 971, 740]))] Commented Aug 30, 2017 at 10:56
  • I am not going through your code and fix the problem for you. At some point you append a tuple of sets to a list. You have to simply find out where and then convert the sets to lists and use the list.extend method or the list.append method. Commented Aug 30, 2017 at 11:12

2 Answers 2

1

The problem is that you're appending a list to a list. You can either iterate over the list you want to add:

items_to_add = ori[0:min(len(ori), 
 num_accepted-len(list_accepted_outsidenestant))]
for item in items_to_add:
 list_accepted_outsidenestant.append(item)

Or add the lists:

list_accepted_outsidenestant = list_accepted_outsidenestant + ori[0:min(len(ori), num_accepted-len(list_accepted_outsidenestant))]

Or as bruno pointed out (even better), extend the list.

list_accepted_outsidenestant.extend(ori[0:min(len(ori), num_accepted-len(list_accepted_outsidenestant))])
answered Aug 30, 2017 at 10:34
Sign up to request clarification or add additional context in comments.

6 Comments

or use list.extend(other_list).
Did not know this. Thanks
@JanZeiseweis I modified the code as suggested using extent. But the problem still persists
Than your problem might be the ori list. Where do you create this. Did you try to print ori before adding it to your existing list?
Python also has the += operator which, if not as efficient as .extend(), is nearly so. (It's a shortcut for list.__iadd__().
|
0

append function add whole into other array
extend function extend add array into previous array

In [1]: a = [1,2,3,4]
In [2]: b = [10,9,8,7,6]
In [3]: a.append(b)
In [4]: a
Out[4]: [1, 2, 3, 4, [10, 9, 8, 7, 6]]
In [5]: c = [1,2,3,4]
In [6]: c.extend(b)
In [7]: c
Out[7]: [1, 2, 3, 4, 10, 9, 8, 7, 6]

Hope this code help you

answered Aug 30, 2017 at 10:46

2 Comments

I understood what you mentioned. I tried to use extend instead of append, but still the problem persists
can please explain what are trying in bit simpler way, because your code is very difficult to read.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.