-1

I got a mission to get an input with only a list and convert it to the output in a way that only the even objects would be in it, and the last one to be with 'and' before it.

I did it that way:

def format_list(my_list):
    even_list = my_list[1:-1:2]
    even_list = str(even_list)

    and_to_end = 'and'

    last_list = my_list[-1]
    last_list = str(last_list)
    total_end = f'{and_to_end} {last_list}'
    full_even_list = f'{even_list} {total_end}'

    return full_even_list


print(format_list(["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"]))

output:

['helium', 'beryllium'] and magnesium

and I want the output to look like this:

hydrogen, lithium, boron, and magnesium

how can i do it?

0stone0
  • 21,605
  • 3
  • 29
  • 49

0 Answers0