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')
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')
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']