-4

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
  • 14,140
  • 41
  • 164
  • 279

1 Answers1

2

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,308
  • 1
  • 6
  • 22
  • 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