DEV Community

Cover image for How to group the values of list elements into other lists with Python
Gideon Akinsanmi
Gideon Akinsanmi

Posted on

How to group the values of list elements into other lists with Python

In this article, I'll be showing you how to implement a python algorithm that pushes the values of list items into another list.

I'd also be showing how to apply this algorithm in solving real-world problems.

First, let's assume you have a blog that publishes sport news. And you want to implement a functionality that tells your blog readers the teams that won and lost matches during the week.

Let's say your data structure is in this form:

#home team name, away team name, result
Matches = [
    ['Chelsea', 'Brighton', 'home team win'],
    ['Burnley', 'Arsenal', 'away team win'],
    ['Man city', 'Sheffield', 'home team win']
]
Enter fullscreen mode Exit fullscreen mode

And you want the result to be like this:

team_win = ['Chelsea', 'Arsenal', 'Man city']
team_lost = ['Brighton', 'Burnley', 'Sheffield']
Enter fullscreen mode Exit fullscreen mode

This is how you implement it in python:

First, create 3 variables: matches, team_win, team_lost.

#home `team name`, `away team name`, `result`
matches = [
    ['Chelsea', 'Brighton', 'home team win'],
    ['Burnley', 'Arsenal', 'away team win'],
    ['Man city', 'Sheffield', 'home team win']
]
team_win = []
team_lost = []
Enter fullscreen mode Exit fullscreen mode

Next, you'll you a for loop to iterate over the values of matches variable.

for i in matches:
Enter fullscreen mode Exit fullscreen mode

Within the loop, If the third item (result) of the list element is 'home team win', append the first item(home team name) of the list element to team_win variable. Else, append it to the team_lost variable

for i in matches:
    if i[2] == 'home team win':
        team_win.append(i [0])
    else:
        team_lost.append(i [0])
Enter fullscreen mode Exit fullscreen mode

If the third item (result) of the list element is 'away team win', append the second item(away team name) of the list element to team_win variable. Else, append it to the team_lost variable

for i in matches:
    #...
    if i[2] == 'away team win':
        team_win.append(i [1])
    else:
        team_lost.append(i [1])
Enter fullscreen mode Exit fullscreen mode

At the end, this is how your code should be look like:

#home team name, away team name, result
matches = [
    ['Chelsea', 'Brighton', 'home team win'],
    ['Burnley', 'Arsenal', 'away team win'],
    ['Man city', 'Sheffield', 'home team win']
]
team_win = []
team_lost = []

for i in matches:
    if i[2] == 'home team win':
      team_win.append(i[0])
    else:
      team_lost.append(i [0])

    if i[2] == 'away team win':
      team_win.append(i[1])
    else:
      team_lost.append(i [1])

print(team_win)
#['Chelsea', 'Arsenal', 'Man city']
print(team_lost)
#['Brighton', 'Burnley','Sheffield']
Enter fullscreen mode Exit fullscreen mode

You can try implementing this algorithm for the following scenarios:
1) separate male and female products from a list containing product information

2) separate months where the sales is greater than 100,000 from those whose sales is lesser than 100,000

3) separate debtors from creditors

I hope you've understood how to implement this algorithm. Cheers.

Top comments (0)