0

I'm new to Python and still learning. I was wondering if there was a standard 'best practice' for storing more than one key value in a tuple. Here's an example:

I have a value called 'red' which has a value of 3 and I need to divide it by a number (say 10). I need to store 3 values: Red (the name), 3 (number of times its divides 10) and 1 (the remainder). There are other values that are similar that will need to be included as well, so this is for red but same results for blue, green, etc. (numbers are different for each label).

I read around and I think way I found was to use nested lists, but I am doing this type of storage for a billion records (and I'll need to search through it so I thought maybe nested anything might slow me down).

I tried to create something like {'red':3:1,...} but its not the correct syntax and I'm considering adding a delimiter in the key value and then splitting it but not sure if that's efficient (such as {'red':3a1,..} then parse by the letter a).

I'm wondering if there's any better ways to store this or is nested tuples my only solution? I'm using Python 2.

agf
  • 171,228
  • 44
  • 289
  • 238
Lostsoul
  • 25,013
  • 48
  • 144
  • 239
  • 1
    If you can't get the syntax right, you need to go through a Python tutorial or two, or browse the [Standard Types](http://docs.python.org/library/stdtypes.html) page of the docs. A dictionary like the one Mark suggested is almost certainly the right datatype for this. – agf Sep 20 '11 at 06:48
  • no no it wasn't so much the syntax with the tuple but a question of how to nest more than 2 values in it. I didn't know I could nest a list within the tuple(as Mark Byers suggested). I think I saw so many examples of tuples being nested in tuples that I didn't realize you could nest other things within it. I will check out the doc you posted though, Thanks agf. – Lostsoul Sep 20 '11 at 06:52
  • Mark suggested you nest a list in a _dictionary_, not a tuple -- otherwise, seems like you get it. – agf Sep 20 '11 at 06:53
  • oops my mistake. Thanks for pointing that out agf. Sorry I posted my code from where I declare the variables and its being entered as dictionaries but I'm converting it to tuples because I need the sort order to be preserved. – Lostsoul Sep 20 '11 at 06:58
  • There is also an `OrderedDict` type in Python 2.7+, if that helps – agf Sep 20 '11 at 07:24
  • wow! I didn't know that command even existed. It looks very applicable to some of my applications. Thank you very much, agf. I thought I knew it all after reading a few books but maybe I need to go through the python official stuff to find gems like these. – Lostsoul Sep 20 '11 at 07:37

3 Answers3

3

The syntax for tuples is: (a,b,c).

If you want a dictionary with multiple values you can have a list as the value: {'red':[3,1]}.

You may want to also consider named tuples, or even classes. This will allow you to name the fields instead of accessing them by index, which will make the code more clear and structured.

I read around and I think way I found was to use nested lists, but I am doing this type of storage for a billion records(and I'll need to search through it so I thought maybe nested anything might slow me down).

If you have a billion records you probably should be persisting the data (for example in a database). You will likely run out of memory if you try to keep all the data in memory at once.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • I had something similar to create the tuple but that only works for one entry. But I have other entries(that I was hoping would look something like: {'red':4:1, 'green':2:0} ) but that syntax isn't obviously correct. I thought of creating a class to append to the tuple but that won't work with the above syntax as well. – Lostsoul Sep 20 '11 at 06:44
  • awesome thanks for the great answer. I just saw your update. I didn't realize I could just use a list within the tuple. Thats amazing. I believe it will work as long as lists retain their order(I know dictionaries do not which is why I am using tuples). I think using your approach I can just refer to the remainder as key[1][1] and get the value(and add other values as well if I need to). Is that logic correct? – Lostsoul Sep 20 '11 at 06:49
  • @Lostsoul if you had `mydict = {'red':[3, 1]}`, you would reference the remainder as `mydict['red'][1]`. – agf Sep 20 '11 at 06:54
1

Use tuple. For example:

`('red', 3, 1)`
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
0

Perhaps you mean dictionaries instead of tuples?

{'red': [3,1], 'blue': [2,2]}

If you are trying to store key/value pairs the best way would be to store them in a dictionary. And if you need more than one value to each key, just put those values in a list.

I don't think you would want to store such things in a tuple because tuples aren't mutable. So if you decide to change the order of the quotient and remainder (1, 3) instead of (3,1), you would need to create new tuples. Whereas with lists, you could simply rearrange the order.

skb
  • 21
  • 2
  • Thanks for the answer SKB. I was using dictionaries before but had to switch to tuples. Basically I need them to retain a specific sort order, and I don't think you can keep dictionaries in a specific order. So the data is entered as a dictionary but I convert it to a tuple. – Lostsoul Sep 20 '11 at 06:56