0

Python - Adding a list to a list

Hello, I am new to python and was wondering how I would code to add a person to a list. E.g i have a big list with all the clubs in it. I’d ask the user to input their name and a club. If the person’s name is in a club, it would add the person to the club List but if the club does not exist, it would say the club doesn’t exist. If the person gets added to a club e.g Choir, I want to also be able to Print the masterlist of all the people In all the clubs, not just a person In the Choir club. We have to do this without using classes and objects. Is this possible? Thanks :)

7th Aug 2019, 7:18 AM
Luna
3 odpowiedzi
+ 4
Not entirely sure what you’re asking, but a large list of lists can get hard to manage, you may instead consider using a dictionary of lists, here are a few examples and methods to get your juices flowing. clubs = {"Choir":[],"Chess":[]} clubs["Choir"].append("John") print("John" in clubs["Chess"]) print("John" in clubs["Choir"]) print(clubs) print(clubs.keys()) print(clubs.values()) print(clubs.get("Choir"))
7th Aug 2019, 7:37 AM
Jake
Jake - avatar
+ 1
Unclear. You would add a person to a club list if this club list has that person in it? Or do you have any other lists of persons to refer to?
7th Aug 2019, 7:30 AM
strawdog
strawdog - avatar
+ 1
I'd do it like Jake, with a dictionary for the clubs, because then you can access them by name.
7th Aug 2019, 7:45 AM
HonFu
HonFu - avatar