Practical Examples of List Grouping
In this section, we'll explore some practical examples of list grouping using custom functions. These examples will demonstrate how list grouping can be applied to various data processing and analysis tasks.
Grouping a List of Persons by Age
Let's consider a list of persons with their names and ages:
persons = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 32},
{"name": "Charlie", "age": 19},
{"name": "David", "age": 45},
{"name": "Eve", "age": 28},
]
We can define a custom function to group the persons by their age:
def group_by_age(person):
return person["age"] // 10 * 10 ## Group by decade
Applying this function to the list of persons:
grouped_persons = {}
for person in persons:
age_group = group_by_age(person)
if age_group not in grouped_persons:
grouped_persons[age_group] = []
grouped_persons[age_group].append(person)
print(grouped_persons)
This will output:
{10: [{"name": "Charlie", "age": 19}],
20: [{"name": "Alice", "age": 25}, {"name": "Eve", "age": 28}],
30: [{"name": "Bob", "age": 32}],
40: [{"name": "David", "age": 45}]}
Grouping a List of Strings by Length
Consider a list of strings:
words = ["apple", "banana", "cherry", "date", "elderberry"]
We can define a custom function to group the words by their length:
def group_by_length(word):
return len(word)
Applying this function to the list of words:
grouped_words = {}
for word in words:
word_length = group_by_length(word)
if word_length not in grouped_words:
grouped_words[word_length] = []
grouped_words[word_length].append(word)
print(grouped_words)
This will output:
{5: ["apple", "banana", "cherry", "date"], 10: ["elderberry"]}
Grouping a List of Transactions by Category
Imagine you have a list of financial transactions with their amounts and categories:
transactions = [
{"amount": 50.0, "category": "groceries"},
{"amount": 25.0, "category": "entertainment"},
{"amount": 75.0, "category": "groceries"},
{"amount": 30.0, "category": "transportation"},
{"amount": 20.0, "category": "entertainment"},
]
We can define a custom function to group the transactions by their category:
def group_by_category(transaction):
return transaction["category"]
Applying this function to the list of transactions:
grouped_transactions = {}
for transaction in transactions:
category = group_by_category(transaction)
if category not in grouped_transactions:
grouped_transactions[category] = []
grouped_transactions[category].append(transaction)
print(grouped_transactions)
This will output:
{'groceries': [{'amount': 50.0, 'category': 'groceries'},
{'amount': 75.0, 'category': 'groceries'}],
'entertainment': [{'amount': 25.0, 'category': 'entertainment'},
{'amount': 20.0, 'category': 'entertainment'}],
'transportation': [{'amount': 30.0, 'category': 'transportation'}]}
These examples demonstrate the versatility of list grouping using custom functions. By defining your own grouping logic, you can tailor the grouping process to suit your specific data processing and analysis requirements.