-3

Is there a way to make the creation of new list more elegant?

my_list = ['apple','orange']
new_list = my_list.copy()
new_list.append('grapes')
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315

1 Answers1

3

You can use the splat * (aka "iterable unpacking") operator to spread the list into a new list literal that includes the other item(s) you want to add, and initialize the new variable with that list.

my_list = ['apple','orange']
new_list = [*my_list, 'grapes']
zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
  • 2
    Related, see [proper name for python * operator](https://stackoverflow.com/questions/2322355/proper-name-for-python-operator). – jarmod Jun 22 '21 at 13:21