Search code examples
pythonlistembedding

Python - List within a list


I'm creating a program in python where i need to ask the user for there team name and team members. I need to have the team names in a list and have the team members embedded within the team name. Any Help?

# Creating Teams

print("Welcome to the program")
print("======================================================")

Teams = []


# Looping to find out the four different team names and players.
for x in range(0, 4) :

    # Asking for the Team name and the team players names
    TeamName = input("What is the name of the team?")
    Player1 = input("What is the first name of the player in your team?")
    Player2 = input("What is the second name of the player in your team?")
    Player3 = input("What is the third name of the player in your team?")
    Player4 = input("What is the last name of the player in your team?")

    Teams.extend([TeamName, Player1, Player2, Player3, Player4])


TeamPlayers = int(input("What Team do you want to see? (0-4)"))

print([Teams, Player1, Player2, Player3, Player4])

Solution

  • I would highly recommend using a dictionary here. Dictionaries allow for easy lookup of keys, and you could easily view the rosters for your teams:

    print("Welcome to the program")
    print("======================================================")
    
    teams = {}
    
    # Looping to find out the four different team names and players.
    for x in range(0, 2) :
        # Asking for the Team name and the team players names
        name = input("What is the name of the team?: ")
        teams[name] = {}
        teams[name]['Player1'] = input("What is the first name of the player in your team?: ")
        teams[name]['Player2'] = input("What is the second name of the player in your team?: ")
        teams[name]['Player3'] = input("What is the third name of the player in your team?: ")
        teams[name]['Player4'] = input("What is the last name of the player in your team?: ")
    
    print(teams)
    

    Output:

    Welcome to the program
    ======================================================
    What is the name of the team?: Team1
    What is the first name of the player in your team?: Chris
    What is the second name of the player in your team?: Chris
    What is the third name of the player in your team?: Chris
    What is the last name of the player in your team?: Chris
    What is the name of the team?: Team2
    What is the first name of the player in your team?: Chris
    What is the second name of the player in your team?: Chris
    What is the third name of the player in your team?: Chris
    What is the last name of the player in your team?: Chris
    {'Team1': {'Player1': 'Chris',
               'Player2': 'Chris',
               'Player3': 'Chris',
               'Player4': 'Chris'},
     'Team2': {'Player1': 'Chris',
               'Player2': 'Chris',
               'Player3': 'Chris',
               'Player4': 'Chris'}}
    

    If you wanted to view all the players on Team1, you could simply use teams['Team1']