Appending item to a sublist based on its first item


my first topic here.

I have a list like:

a = [['fruit','banana','apple'],['insect','ant','bee',wasp']]

I want to add an item to the list based on its category. For example, if the new item is 'butterfly', I want to add it to the list after checking that the 'insect' sublist exists, so that a becomes:

a = [['fruit','banana','apple'],['insect','ant','bee',wasp','butterfly']]

However, if the category is 'car' and the item is 'ferrari', I want to create a new sublist called 'cars', and a 2nd subitem 'ferrari', so that a becomes:

a = [['fruit','banana','apple'],['insect','ant','bee',wasp'],['car','ferrari']]

In other words, the category is the first item of each sublist, and is followed by all the items of that category.

Once I completed building the list, I would like to print it like this:

fruit: banana, apple
insect: ant, bee, wasp, butterfly
car: ferrari

What sort of code I need to do that quickly?

And BTW, is a list the best way to deal with this kind of problem? Maybe better to use another way to store those values?

Many thanks for your help.

I would have thought that a dictionary is the way to go:

data_base = {'fruit': ['banana', 'apple'],
             'insect': ['ant', 'bee', 'wasp']

if 'insect' in data_base:
    if 'butterfly' not in data_base['insect']:
    data_base['insect'] = ['butterfly']

if 'car' in data_base:
    if 'ferrari' not in data_base['car']:
    data_base['car'] = ['ferrari']

Clearly, the data in this script would come from some user input, rather than being hard coded and there’s no need for the two if/else loops; this simply serves to show the concept.

1 Like

A dictionary of lists is a better data structure here

d = {'fruit': ['banana', 'apple'], 'insect': ['ant', 'bee', 'wasp']}

Assuming you have a list of category-item pairs such as

pairs = [('fruit', 'banana'),
         ('insect', 'ant'),
         ('insect', 'bee'),
         ('fruit', 'apple'),
         ('insect', 'wasp'),
         ('insect', 'butterfly'),
         ('car', 'ferrari')]

You can create the above dictionary by doing

d = {}
for category, item in pairs:
    if category not in d:
        d[category] = []

Or more succinctly as

d = {}
for category, item in pairs:
    d.setdefault(category, []). append(item)

Where d now contains

{'fruit': ['banana', 'apple'], 'insect': ['ant', 'bee', 'wasp', 'butterfly'], 'car': ['ferrari']}
1 Like

Yes a dict is the right datastructure to do this rather than a list of lists. Perhaps even a dict of sets.

In fact, a defaultdict should work too.

1 Like

Relevant on Stack Overflow:

I am quite new to python. So, it took some time for me to make it works in my script and fully understand dictionary, but at the end it worked.

Thanks very much to you in particular, but also to everyone else that commented here.

1 Like

Thanks again for the answers.

However, I still can’t find a way to print ALL the values of ALL the keys in the following format:

fruit: banana, apple
insect: ant, bee, wasp, butterfly
car: ferrari

I probably didn’t understand dictionaries yet…

Iterate through the dictionary keys, using a for loop. Within the loop, display each key followed by a colon and a space, and use the key to access the associated list, which needs to be formatted for display. Assuming the name of the dictionary is database, that can be accomplished as follows:

for category in database:
    print(f"{category}: {', '.join(database[category])}")
1 Like