Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent for C++ STL vector/list containers

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.

like image 427
pandoragami Avatar asked Jan 09 '11 00:01

pandoragami


People also ask

Is vector in C++ same as list in Python?

Vectors are much more similar to Python lists than arrays are. Vectors use a dynamically allocated array to store their elements, so they can change size, and they have other friendly features as well.

Is Python list same as vector?

Vector may have a default size. List does not have default size. In vector, each element only requires the space for itself only. In list, each element requires extra space for the node which holds the element, including pointers to the next and previous elements in the list.

Is vector an stl container?

The STL vector Container. The vector class implements a dynamic array that provides fast insertion at the end, fast random access to its components, and automatically resizes itself when components are inserted or deleted.


1 Answers

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.

like image 83
Johan Kotlinski Avatar answered Sep 21 '22 01:09

Johan Kotlinski