Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A problem from python crash course by google

Tags:

python

list

The group_list function accepts a group name and a list of members, and returns a string with the format: group_name: member1, member2, … For example, group_list("g", ["a","b","c"]) returns "g: a, b, c". Fill in the gaps in this function to do that.

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:"

I have tried sth like this:

members = " " 
for i in users:
    members += ",".join(i)
return ("{}:{}".format(group, members))

output comes:

Marketing: M,i,k,eK,a,r,e,nJ,a,k,eT,a,s,h,a
Engineering: K,i,mJ,a,yT,o,m
Users: 

but it didn't give the expected answer. Can anyone solve it with filling the blanks please ?

like image 595
Daisk Avatar asked Apr 09 '26 17:04

Daisk


2 Answers

You need to join users, not each user separately.

members = ', '.join(users)
return "{}: {}".format(group, members)
like image 154
wjandrea Avatar answered Apr 11 '26 06:04

wjandrea


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:"
like image 43
Anwar Avatar answered Apr 11 '26 06:04

Anwar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!