22

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pandoragami
  • 5,387
  • 15
  • 68
  • 116

6 Answers6

13

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.

http://effbot.org/zone/python-list.htm

N.B.: Please keep in mind that vector and list are two very different data structures. List are heterogeneous, i.e. can store different object types, while C++ vectors are homogeneous. The data in vectors is stored in linear arrangement whereas in list is a collection of references to the type and the memory address of the variables.

TennisTechBoy
  • 101
  • 10
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
12

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
wheaties
  • 35,646
  • 15
  • 94
  • 131
  • Well, it can be... it depends on the backing implementation. But in CPython, yes, `[]` != `std::list`. – Amber Jan 09 '11 at 03:25
  • 2
    @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. –  Jan 09 '11 at 05:34
  • @delnan There are, however, backing data structures which would be much closer to a `std::list` than the CPython implementation. – Amber Jan 09 '11 at 06:54
  • @Amber: Please elaborate. What data structure is "close to a linked list" but offers e.g. O(1) indexing and appending? –  Jan 09 '11 at 06:56
  • @delnan Certain variants of b-trees don't offer O(1) indexing, but can get fairly close (close enough that all but the largest edge cases wouldn't have a noticeable difference). – Amber Jan 09 '11 at 07:30
  • @Amber: In reality it would be a big difference both in terms of performance and memory use, to the worse. So I don't see why anyone would want to differ from CPython in this case - after all, it's the de facto standard. – Johan Kotlinski Jan 15 '11 at 00:24
  • `set() => std::unordered_set` , since it's hashset – AsukaMinato May 07 '21 at 18:52
6
py cpp
deque deque
PriorityQueue (or you may use heapq) priorityqueue
set unordered_set
list vector
defaultdict(int) unordered_map
list stack
deque queue
dict .get(val,0) unordered_map
array array

in py >= 3.7, dict remember insert order. https://stackoverflow.com/a/51777540/13040423


In case you need TreeMap / TreeSet

https://github.com/grantjenks/python-sortedcontainers

AsukaMinato
  • 1,017
  • 12
  • 21
1

The cstl library wrapped commonly used C++ STL libraries including vector, unordered_map, and unordered_set for Python. It uses purely C++ implementation and does not have the copy-on-write issue that happens in all python objects. See https://github.com/fuzihaofzh/cstl for how to install and use.

Install it from pip

pip install cstl

Convert python objects into cstl objects:

import cstl

# Directly covert containers from python
v = cstl.frompy({"1":[1,2,3], "2":[4,5,6]}) # convert python object to cstl object
v["1"][2] = 10 # access cstl object
pv = cstl.topy(v) # convert cstl object to python object 
print(pv)
maple
  • 1,828
  • 2
  • 19
  • 28
0

Lists are sequences.

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

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

Umut Tabak
  • 1,862
  • 4
  • 26
  • 41
0

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Paul
  • 42,322
  • 15
  • 106
  • 123