-1

Let's say I got a list below:

original = ['a', 'c', 'e']

I want to add another list, called add_item, to the list original:

add_item = ['b', 'd']

The result should be:

['a', 'b', 'c', 'e']

What is the syntactically cleanest way to accomplish this?

Thanks for any help!!

retr0327
  • 141
  • 4
  • 9

3 Answers3

0
original = ['a', 'c', 'e']
add_item = ['b', 'd']
original.extend(add_item) #['a', 'c', 'e', 'b', 'd']
0
original = ['a', 'c', 'e']
add_item = ['b', 'd']

print(sorted(original + add_item))

You can use the overloaded + operator to concatenate lists, and if sorting is important to you you can use the built-in sorted function

ted
  • 13,596
  • 9
  • 65
  • 107
0

Syntactically the cleanest way would be to use extend method

original = ['a','c','e']
original.extend(add_item)