1

Assume I have a "constant" like:

ITEMS = [ "item1", "item2" ]

Now I would like to create an array that contains all entries of ITEMS, plus some more.

Obviously, this is wrong:

more_things = [ "all", ITEMS ]

as it would put the whole array into the new array. But I want the new array to contain [ "all", "item1", "item2" ] in the end.

Sure, I could somehow iterate the first array, but I am wondering if there is a more idiomatic one liner way to do this. Ideally, it should work for python2 and python3.

0

5 Answers 5

2

Just concatenate them with +:

more_things = ["all"] + ITEMS
Sign up to request clarification or add additional context in comments.

3 Comments

@GhostCat yep, works for both major versions of Python; I didn't downvote, but IMO people are more likely to downvote this for the abundance of duplicates than its simplicity.
@GhostCat StackOverflow works in mysterious ways, but if I had to guess it could be fear of retribution
Ah, well. I really dont mind simple (correct) close as dup votes. If there would have been one early on,I just delete the question and move on. But comment-less downvotes, that sucks. And I think hope assume that I dont have a reputation of going revenging things ...
2

If you're running Python 3.5+, the closest to your request is to use unpacking:

>>> ITEMS = ['item1', 'item2']
>>> ['all', *ITEMS]
['all', 'item1', 'item2']

In earlier versions list concatenation would do the same:

>>> ITEMS = ['item1', 'item2']
>>> ['all'] + ITEMS
['all', 'item1', 'item2']

If you simply need an iterable (i.e. not necessarily a list), using itertools.chain() may be more efficient:

from itertools import chain
more_things = chain(['all'], ITEMS)  # or wrap in list() to get a list

Comments

1

In Python 3 you can do:

ITEMS = [ "item1", "item2" ]
more_things = [ "all", *ITEMS ]

This is quite flexible because it lets you combine several lists (or iterables in general) and elements in one line:

even_more_things = [ "all", *ITEMS, "the", *ITEMS, "items" ]

Comments

1

I'll suggest one more obvious approach: list.insert.

ITEMS.insert(0, 'all')
ITEMS
# ['all', 'item1', 'item2']

It modifies ITEMS in-place.


If you don't want to modify ITEMS, you can create a copy... although at that point, you'd rather one of the simpler options like iterable unpacking (*)...

ITEMS2 = ITEMS.copy()
ITEMS2.insert(0, 'all')

ITEMS2
# ['all', 'item1', 'item2']

1 Comment

ITEMS is supposed to be a "constant". more_things on the other hand is supposed to go into a choices array for argparse. So my question is really about creating a new array, without changing the first one. But sure, it is worth noting that changing is possible, too.
0

You can also use itertools.chain

from itertools import chain

more_things = list(chain(more_things, ITEMS))

it is useful when you want to add more than two lists together.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.