# unordered_map<int, vector<float>> equivalent in Python

## Question:

I need a structure in Python which maps an integer index to a vector of floating point numbers. My data is like:

`````` = {1.0, 1.0, 1.0, 1.0}
 = {0.5, 1.0}
``````

If I were to write this in C++ I would use the following code for define / add / access as follows:

``````std::unordered_map<int, std::vector<float>> VertexWeights;
VertexWeights.push_back(0.0f);
vertexWeights.push_back(1.0f);
vertexWeights.push_back(0.5f);
std::cout <<vertexWeights;
``````

What is the equivalent structure of this in Python?

I would go for a `dict` with integers as keys and `list` as items, e.g.

``````m = dict()
m = list()
m.append(1.0)
m.append(0.5)
m = list()
m.append(13.0)
``````

if it is not too much data

How about dictionary and lists like this:

``````>>> d = {0: [1.0, 1.0, 1.0, 1.0], 1: [0.5, 1.0]}
>>> d
[1.0, 1.0, 1.0, 1.0]
>>> d
[0.5, 1.0]
>>>
``````

The key can be integers and associated values can be stored as a list. Dictionary in Python is a hash map and the complexity is amortized `O(1)`.

A dictionary of this format -> `{ (int) key : (list) value }`

``````d = {}  # Initialize empty dictionary.
d = [1.0, 1.0, 1.0, 1.0] # Place key 0 in d, and map this array to it.
print d
d = [0.5, 1.0]
print d
>>> [1.0, 1.0, 1.0, 1.0]
>>> [0.5, 1.0]
print d  # std::cout <<vertexWeights;
>>> 1.0
``````

In python we can this data structure as Dictionary.
Dictionaries are used to store data values in key:value pairs.
Example for Dictionary:
mydict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
we can also perform various operations like add, remove.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.