Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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

Browse Categories

...