0 votes
1 view
in Python by (47.8k points)

list.append() is the obvious choice for adding to the end of a list. Here's a reasonable explanation for the missing list.prepend(). Assuming my list is short and performance concerns are negligible, is

list.insert(0, x)

or

list[0:0] = [x]

idiomatic?

1 Answer

0 votes
by (107k points)

There are many ways to pretend to a short python list some of the important ways are as follows:-

The first method you can use s.insert(0, x) it is the most common whenever you see it, it may be time to consider using a collections.deque instead of a list.

Another way is the functional way, we do it as follows:-

new_list = [x] + your_list

Related questions

0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.8k points)
+1 vote
2 answers
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...