0

I'm trying to convert this into a list comprehension.

Current Code

some_list = [[{"apple":"red"}, {"orange":"orange"}, {"pineapple":"yellow"}, [{"box":"brown"}, {"table":"grey"}, {"seat":"green"}], {"grapes":"purple"}, {"cherry":"red"}]]

category_list = []
for item in some_list:
    tag_list = []
    for elem in item:
        if isinstance(elem, list):
            for x in elem:
                tag_list.append(x)
        else:
            tag_list.append(elem)
            
    category_list.append(tag_list)
    
category_list

Desired Output

[[{'apple': 'red'},
  {'orange': 'orange'},
  {'pineapple': 'yellow'},
  {'box': 'brown'},
  {'table': 'grey'},
  {'seat': 'green'},
  {'grapes': 'purple'},
  {'cherry': 'red'}]]

My Attempt

[[x for x in elem] if isinstance(elem, list) else elem for item in some_list for elem in item]

Results

[{'apple': 'red'},
 {'orange': 'orange'},
 {'pineapple': 'yellow'},
 [{'box': 'brown'}, {'table': 'grey'}, {'seat': 'green'}],
 {'grapes': 'purple'},
 {'cherry': 'red'}]

But this just gets me right back to where I started.

6
  • 4
    "But this just gets me right back to where I started"—how are your input and desired output different? They appear to be identical. Edit: Oh, I see. There's a third nested inner list in the input containing "box", "table", and "seat". This is exactly the kind of thing that would be helpful to have called out explicitly in your question. Make it easy for us to help you. See How to Ask. Commented Dec 14, 2020 at 22:51
  • 2
    Two of the design goal of list comprehension are the reduction of complexity and the increase of readability. You seem to walk in the opposite direction. Commented Dec 14, 2020 at 22:55
  • @Chris Sorry, I just updated my results to show the difference. Commented Dec 14, 2020 at 23:00
  • @KlausD.Sorry my question wasn't "What is the design goal of list comprehensions?" Again, my sincerest apologies this wasn't clear when I wrote: "I'm trying to convert this into a list comprehension." Commented Dec 14, 2020 at 23:03
  • you want to remove the third '[' and the first ']'? no? so maybe what you want to do is not so clear?! Commented Dec 14, 2020 at 23:13

1 Answer 1

2

Try out this comprehension:

some_list = [[{"apple":"red"}, {"orange":"orange"}, {"pineapple":"yellow"}, [{"box":"brown"}, {"table":"grey"}, {"seat":"green"}], {"grapes":"purple"}, {"cherry":"red"}]]

categories = [x if type(x) == dict else y for x in some_list[0] for y in x]

print(categories)
# Prints 
# [{'apple': 'red'}, {'orange': 'orange'}, {'pineapple': 'yellow'}, 
# {'box': 'brown'}, {'table': 'grey'}, {'seat': 'green'}, 
# {'grapes': 'purple'}, {'cherry': 'red'}]

This appends the element if it's a dictionary, otherwise appends each element in the element (which would be a list).

Note: I'm actually new to list comprehension so let me know if I made any mistakes!

Sign up to request clarification or add additional context in comments.

Comments

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.