Take the 2-minute tour ×

Is there something similar in Python that I would use for a container that's like a vector and a list?

Any links would be helpful too.

share|improve this question
For linked list: [a0,[a1,[a2,[a3,[...]]]]] –  Kabie Jan 9 '11 at 4:52

4 Answers 4

up vote 5 down vote accepted

You can use the inbuilt list - underlying implementation is similar to C++ vector. Although some things differ - for example, you can put objects of different type in one and the same list.


share|improve this answer
Is accessing linear in time O(1), similar to accessing a vector as an array in C++? –  lost_with_coding Jan 9 '11 at 1:02
Yes, underlying implementation is like a C++ vector. –  kotlinski Jan 9 '11 at 1:04
That arrays are called lists (which is genereally used as shortcut for linked list, which is a wholly different data structure) is one of the few really unfortunate things in Python. –  delnan Jan 9 '11 at 1:06
Thanks, it has the performance factors on the bottom, good info! –  lost_with_coding Jan 9 '11 at 1:13
This answer is incomplete for being accepted: the list class is nothing like std::list. (There's no builtin linked list implementation in Python.) –  Glenn Maynard Jan 9 '11 at 2:33

Have a look at Python's datastructures page. Here's a rough translation:

  1. () => boost::Tuple (with one important distinction, you can't reassign values in a Python tuple)
  2. [] => std::vector (as the comments have aluded towards, lacks memory characteristics associated with vectors)
  3. [] => std::list
  4. {} => tr1::unordered_map or boost::unordered_map (essentially a hash table)
  5. set() => std::set
share|improve this answer
[] != std::list. –  Glenn Maynard Jan 9 '11 at 2:34
Well, it can be... it depends on the backing implementation. But in CPython, yes, [] != std::list. –  Amber Jan 9 '11 at 3:25
@Amber: No Python implementation would ever dare to use linked lists for the builtin list type. That would totally screw about every piece of code that relies on indexing being O(1) (a totally valid assumption) - i.e. really much. We can safely ignore this scenario. –  delnan Jan 9 '11 at 5:34
@delnan There are, however, backing data structures which would be much closer to a std::list than the CPython implementation. –  Amber Jan 9 '11 at 6:54
@Amber: Please elaborate. What data structure is "close to a linked list" but offers e.g. O(1) indexing and appending? –  delnan Jan 9 '11 at 6:56

Lists are sequences.

see http://docs.python.org/tutorial/datastructures.html

append is like push_back, see the other methods as well.

share|improve this answer

Python also has as part of the standard library an array type which is more efficient and the member type is constrained.

You may also look at numpy (not part of the standard library) if you need to get serious about efficient manipulation of large vectors/arrays.

share|improve this answer

Your Answer


