my_list = ['a','b','c']
new_item = 'd'
newest_item = 'e'
How can i get?
result = ['a','b','c','d','e']
My attempt was:
result = [x for x in my_list,new_item,newest_item]
Along with @mdml's solution, you can also simply use append
>>> my_list = ['a','b','c']
>>> my_list.append('d')
>>> my_list
['a', 'b', 'c', 'd']
>>> my_list.append('e')
>>> my_list
['a', 'b', 'c', 'd', 'e']
.extend(('d', 'e')).You can use so many ways to achieve this result.
First of, you can append items to your list:
my_list = ['a','b','c']
my_list.append('d') -> ['a','b','c','d']
my_list.append('e') -> ['a','b','c','d', 'e']
It is handy if you need to add items in cycle.
If you have few items and you want to add all of then to list:
my_list = ['a','b','c']
my_list.extend(['d', 'e']) -> ['a','b','c','d', 'e']
Or
my_list += ['d', 'e']