Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Lists Using Lists Multidimensional Lists

how to .remove from a multidimensional list?

was just playing around with lists and not sure how to .pop/.remove/ or del a string from a list within a multidimensional list?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

It can be fairly straightforward. A list is an ordered container of objects. If those objects happen to also be lists then it behaves like a multiple-dimensional array. Since .remove() and pop() are methods of a particular list instance, you need only to refer that instance to call the method.

>>> mlist = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]]
>>> mlist[1].pop()
0
>>> mlist[0].pop(2)
3
>>> mlist[1].remove(8)
>>> mlist
[[1, 2, 4, 5], [6, 7, 9]]

Post back if you need more help. Good luck!!!

Thanks Chris! I understand the above but Im struggling to get it to work in some code.

def teamLeague(league):
    if league == "prem":
        for team in teams[0]:
            print(team)
    elif league == "laLiga":
        for team in teams[1]:
                print(team)
    else:
        for team in teams[2]:
            print(team)

def removeTeam(teamRemoved):
    teams[0].remove(teamRemoved)


teams = [
    ["arsenal", "chelsea", "villa"],
    ["barca", "real", "athletico"],
    ["bayern", "dortmund", "wolfsburg"],
]

q = input("what league, prem laLiga or bundesliga? ")
teamLeague(q)
q2 = input("remove a team. ")
removeTeam(q2)
print(teams)

I am trying to remove the chosen str and then print the altered list but can't get the removeTeam function working. This block works but not sure how to add to the function so it works for all options. have tried different methods using for loops and if statements but no correct answer.

Thanks!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

There is a loose coupling of the league names with the index values. This coupling is only present in teamLeague where

prem=0, laLiga=1, and presumably bundesliga=2

The issue is when you get a team name in q2, you’ve lost the team index value.

One way around this is to define a β€œdecoder” that translates between team names and index values. For example:

league_to_index = {
    prem: 0,
    laLiga: 1,
    Bundesliga: 2,
    }

The teamLeague could be reduced to

for team in teams[league_to_index(league)]:
    print(team)

Then the team removal could be:

teams[league_to_index(q)].remove(q2)

A more advanced way to enumerate the indexing would be to use Enum types.

Post back if you need more help. Good luck!!!