Ever since you started work at the grocer, you have been faithfully logging down each item and its category that passes through. One day, your boss walks in and asks, "Why are we just randomly placing the items everywhere? It's too difficult to find anything in this place!" Now's your chance to improve the system, impress your boss, and get that raise!

The input is a comma-separated list with category as the prefix in the form `"fruit_banana"`. Your task is to group each item into the 4 categories `Fruit, Meat, Other, Vegetable` and output a string with a category on each line followed by a sorted comma-separated list of items.

For example, given:

```
"fruit_banana,vegetable_carrot,fruit_apple,canned_sardines,drink_juice,fruit_orange"
```

output:

```
"fruit:apple,banana,orange\nmeat:\nother:juice,sardines\nvegetable:carrot"
```

Assume that:
1. Only strings of the format `category_item` will be passed in
2. Strings will always be in lower case
3. Input will not be empty
4. Category and/or item will not be empty
5. There will be no duplicate items
6. All categories may not have items

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Starter code: def group_groceries(groceries):
	

Solutions:
def group_groceries(groceries):
	categories = {'fruit': [], 'meat': [], 'other': [], 'vegetable': []}
	for entry in groceries.split(','):
		(category, item) = entry.split('_')
		categories[category if category in categories else 'other'].append(item)
	return '\n'.join([f"{category}:{','.join(sorted(items))}" for (category, items) in categories.items()])


def group_groceries(groceries):
	categories = {'fruit': [], 'meat': [], 'other': [], 'vegetable': []}
	for product in groceries.split(','):
		(key, value) = product.split('_')
		categories[key if key in categories else 'other'].append(value)
	return '\n'.join(('{}:{}'.format(cat, ','.join(sorted(items))) for (cat, items) in categories.items()))


def group_groceries(groceries):
	groceries = groceries.split(',')
	carne = []
	fruta = []
	outro = []
	vegetal = []
	for item in groceries:
		a = item.split('_')
		if a[0] == 'fruit':
			fruta.append(a[1])
		elif a[0] == 'meat':
			carne.append(a[1])
		elif a[0] == 'vegetable':
			vegetal.append(a[1])
		else:
			outro.append(a[1])
	b = 'fruit:{}\nmeat:{}\nother:{}\nvegetable:{}'.format(','.join(sorted(fruta)), ','.join(sorted(carne)), ','.join(sorted(outro)), ','.join(sorted(vegetal)))
	return b


def group_groceries(groceries, categories='fruit meat other vegetable'.split()):
	groups = {category: [] for category in categories}
	for product in groceries.split(','):
		(category, item) = product.split('_')
		groups.get(category, groups['other']).append(item)
	return '\n'.join((f"{category}:{','.join(sorted(products))}" for (category, products) in groups.items()))


def group_groceries(groceries):
	dict = {'fruit': [], 'meat': [], 'other': [], 'vegetable': []}
	for i in groceries.split(','):
		items = i.split('_')
		if not items[0] in ['fruit', 'meat', 'vegetable']:
			dict['other'].append(items[1])
		else:
			dict[items[0]].append(items[1])
	temp = {k: ','.join(sorted(v)) for (k, v) in dict.items()}
	return '\n'.join((f'{i}:{j}' for (i, j) in temp.items()))


def group_groceries(groce):
	box = {'fruit': [], 'meat': [], 'other': [], 'vegetable': []}
	groce = [e.split('_') for e in groce.split(',')]
	for (k, v) in groce:
		box.get(k, box['other']).append(v)
	return '\n'.join((f"{k}:{','.join(sorted(v))}" for (k, v) in box.items()))


CATEGORIES = ['fruit', 'meat', 'other', 'vegetable']
KNOWN = {c: c for c in CATEGORIES if c != 'other'}

def group_groceries(groceries):
	d = {key: [] for key in CATEGORIES}
	for item in groceries.split(','):
		(category, name) = item.split('_')
		d[KNOWN.get(category, 'other')].append(name)
	return '\n'.join((f"{c}:{','.join(sorted(d[c]))}" for c in CATEGORIES))


def group_groceries(groceries):
	res = {'fruit': [], 'meat': [], 'other': [], 'vegetable': []}
	for item in groceries.split(','):
		(category, name) = item.split('_')
		res.get(category, res['other']).append(name)
	return '\n'.join((f"{k}:{','.join(sorted(v))}" for (k, v) in res.items()))


def group_groceries(groceries):
	groups = ['fruit', 'meat', 'other', 'vegetable']
	d = {}
	for item in groceries.split(','):
		(g, i) = item.split('_')
		g = g if g in groups else 'other'
		try:
			d[g].append(i)
		except:
			d[g] = [i]
	final_string = []
	for g in groups:
		try:
			group_string = g + ':' + ','.join(sorted(d[g]))
		except:
			group_string = g + ':'
		final_string.append(group_string)
	return '\n'.join(final_string)


def group_groceries(s):
	d = {x: [] for x in 'fruit meat other vegetable'.split()}
	for x in s.split(','):
		(a, b) = x.split('_')
		d[a if a in d else 'other'].append(b)
	return '\n'.join((f"{x}:{','.join(sorted(y))}" for (x, y) in d.items()))