NaughtyMike
NaughtyMike

Reputation: 1

saving more than one value in one Python array position (kinda container)

I would like to use something as a container but I can't do objects... I believe there is some library or collection or something which could help my.

I want to save a few connected values into one array position:

   array = []

   array.append(value1 = 1, value2 = 2, value3 = 3)
   array.append(value1 = 5, value2 = 7, value3 = 10)
   array.append(value1 = 2, value2 = 3, value3 = 3)

Something like this... And then I would like to search in this array like

   for n in array:
       n.value1 = ....

But I'm beginner and don't know much about the language... Can you please help me?

Upvotes: 0

Views: 131

Answers (2)

linluk
linluk

Reputation: 1670

you are looking for a dictionary. it can be used like this:

d = {"value1": 1, "value2": 2, "value3": 3}
for k in d:
    print("key: {}, value: {}".format(k, d[k]))

here are the docs: https://docs.python.org/2/tutorial/datastructures.html#dictionaries


for your problem you 'll need a list of dictionaries. like this:

list_of_dict = []

list_of_dict.append({"value1": 1, "value2": 2, "value3": 3})
list_of_dict.append({"value1": 5, "value2": 7, "value3": 10})
list_of_dict.append({"value1": 2, "value2": 3, "value3": 3})

for dct in list_of_dict:
    dct["value1"] = ...

Upvotes: 2

Simon Enström
Simon Enström

Reputation: 1

As mentioned in the comment you are looking for a dictionary; see the docs or this tutorial.

Example code:

    dict = {'value1':1,'value2':2,'value3':3}

Upvotes: -1

Related Questions