2

I've got a list of around 6000 (text) objects, which I am trying to store and pass values from it (after manipulation) to another list. I am using the append and extend functions.

The program works fine and gives me the desired result, but it is too slow.

How can I increase its performance (without using С code in my program)?

aragaer
  • 17,238
  • 6
  • 47
  • 49
Daniel Petrov
  • 95
  • 3
  • 12

1 Answers1

2

You are after the collections module (included with Python).

This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers.

At the top we see:

deque: list-like container with fast appends and pops on either end

from collections import deque

items = deque([1,2,3])
items.pop()
items.extend()
items.append()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
timeyyy
  • 654
  • 9
  • 20
  • 1
    Appending to or popping from the front is a possible reason for slowness, but there is nothing in the question that says that Natasha's code does that. – Terry Jan Reedy Nov 13 '14 at 20:15
  • you must have missed it, _I am using append and extend functions_ – timeyyy Nov 14 '14 at 08:46
  • 1
    'appending to the front' is done with insert(), not append(). extend() is a series of appends to the rear. I see nothing about adding items anywhere other than at the end. – Terry Jan Reedy Nov 14 '14 at 21:59
  • @TerryJanReedy, Natasha seems to have accepted this answer, so it fixed her problem which means you could probably retract your downvote. – Cristian Ciupitu Oct 05 '15 at 20:40