python - Separate tuple from a nested list into a separate list -


i need separate tuple based on value nested dictionary below , put in list. want separate tuple values 'bb'

original_list= [[('aa','1'),('bb','2')],[('cc','3'),('bb','4')],[('dd','5'),('dd','6')]] 

i need 2 lists below,

 final_list= [[('aa','1')],[('cc','3')],[('dd','5'),('dd','6')]]  deleted_list = [[('bb','2')],[('bb','4')]] 

i used following recursive code,

def remove_items(lst, item):     r = []     in lst:         if isinstance(i, list):             r.append(remove_items(i, item))         elif item not in i:             r.append(i)     return r 

it produce result list after deleting value. there way list deleted values?

>>> def remove_items(lst, item): ...     r = [] ...     d = [] ...     in lst: ...         if isinstance(i, list): ...             r_tmp,d_tmp = remove_items(i, item) ...             if r_tmp: ...                 r.append(r_tmp) ...             if d_tmp: ...                 d.append(d_tmp) ...         else: ...                 if item not in i: ...                     r.append(i) ...                 else: ...                     d.append(i) ...     return r,d ...  >>> original_list= [[('aa','1'),('bb','2')],[('cc','3'),('bb','4')],[('dd','5'),('dd','6')]] >>> result = remove_items(original_list,'bb') >>> result[0] [[('aa', '1')], [('cc', '3')], [('dd', '5'), ('dd', '6')]] >>> result[1] [[('bb', '2')], [('bb', '4')]] >>>  

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -