Given a list like this:
sentence = ("george kendall", 7, "wally west", 21, "Joe montana", 17, "Alice Cooper")
and I want to have it like this:
["Alice Cooper", "George Kendall", "Joe Montana", "Wally West", 45]
Note: The first letter of the first name and surname must be in capital letter and at the end of the list is the sum of all integers inside the tuple with all elements sorted alphabetically.
I was able to do most of this tasks with the next code:
def sortArtists(sentence):
l = list(sentence)
list_alpha = []
list_digit = []
for item in l:
if isinstance(item, str):
list_alpha.append(item)
else:
list_digit.append(item)
add = sum(list_digit)
arrange = sorted(list_alpha, key=str.lower)
mystring = str(arrange).title()
list(mystring).append(add)
return mystring
print(sortArtists(("george kendall", 7, "wally west", 21, "Joe montana", 17, "Alice Cooper")))
in theory, in the line list(mystring).append(add)
should add the sum of 45 that is the total sum of the integers inside the list, but nothing happens.
All that I received is: ['Alice Cooper', 'George Kendall', 'Joe Montana', 'Wally West']
but 45
is missing at the end of the list.
If I write in the return
line return mystring + str(add)
, I get as output:
['Alice Cooper', 'George Kendall', 'Joe Montana', 'Wally West']45
but 45
is outside the brackets []
. What can I do to accomplish this?