What do other people do when they put elements in the front of the list?

Asked 2 years ago, Updated 2 years ago, 47 views

I know that the last one is called list.append().

Then, when you put it in the beginning,

Which one should I use?

list python prepend

2022-09-22 12:50

1 Answers

s.insert(0, x)

It's usually written like this.

I don't know what code you're using, but if you're putting an element at the forefront of the list when the speed of performance is important, I recommend you use this class collections.deque([userable[, maxlen])).

As you can see from the document, the insert (0, x) in the list is O(n) class collections.deque([iterable[, maxlen])'s queue() is O(1) so if speed is important, use this one


2022-09-22 12:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.