'how to solve: The group_list function accepts a group name and a list of members
def group_list(group, users):
members = ___
return ___
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
Solution 1:[1]
def group_list(group, users):
members = ', '.join(users)
return group + ': ' + members
Solution 2:[2]
The string.join()
method works well here. Get the members out of the input and into a list, and define a separator:
members = [x for x in users]
separator = ", " # Since we want commas in our string at the end
From here, get the members out of the list and into a string:
memstring = separator.join(members)
From here, you can use string formatting to get the output how you want it.
Solution 3:[3]
def group_list(group, users):
members = ', '.join(users)
return group + ': ' + members
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
Solution 4:[4]
def group_list(group, users):
members = ", ".join(users)
return "{}: {}".format(group, members)
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
Solution 5:[5]
def group_list(group, users):
L = users
return ("{}: ".format(group) + " ".join(L))
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"]))
print(group_list("Engineering", ["Kim", "Jay", "Tom"]))
print(group_list("Users", ""))
Solution 6:[6]
def group_list(group, users):
members = "{}:{}".format(group, " ".join(users) )
return members
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be
"Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be
"Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
Solution 7:[7]
def group_list(group, users):
return f"{group}: {', '.join(users)}"
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
Solution 8:[8]
def group_list(group, users):
members = "{}: {}".format(group, ", ".join(users) )
return members
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow