Question

I'm learning Python Tuple, and feeling a bit overwhelmed. I'm working with a tuple about 20 times the size of what I've put together below.

{u'0UsShTrY': {u'a': {u'n': u'Backups'}, u'h': u'0UsShTrY', u'k': (16147314, 17885416, 1049370661, 902515467), u'ts': 13734967, u'p': u'5RtyGQwS', u'u': u'xpz_tb-YDUg', u't': 1, 'key': (16147314, 17885516, 10490661, 9015467)}, 
u'oMV32IgB': {u'a': {'n': 'Rubbish Bin'}, u'h': u'oMV32IgB', u'k': u'', u'ts': 13734735, u'p': u'', u'u': u'xpz_tb-YDUg', u't': 4}, 
u'AclTQTAa': {u'a': {u'n': u'Test3'}, u'h': u'AclTQTAa', u'k': (4031580, 13207606, 20877418,89356117), u'ts': 1373476935, u'p': u'4FlnwBTb', u'u': u'xpz_tb-YDUg', u't': 1, 'key': (4032580, 13208406, 20627418, 4893117)}, 
u'kEk0RbKR': {u'a': {u'n': u'Abandon All Ships - 5 - Stange Love.mp3'}, u'h': u'kEk0RbKR', u'k': (4714448, 440504, 14565743L, 7910538L), u'ts': 13737027, 'iv': (4284718, 20627111, 0, 0), u'p': u'wQkyFS6S', u's': 1731926, 'meta_mac': (3010404L, 2624700L), u'u': u'xpz_tb-YDUg', u't': 0, 'key': (94654, 201535, 385311L, 301074L, 42818, 204311, 3010404L, 269100L)}}

Now, my issue is, I'm trying to access the data of located in where you see "Test3" or "Abandon All Ships - 5 - Stange Love.mp3", as well as for example where you see "u'p': u'5RtyGQwS'," on the first line. How would I go about accessing these without predefining any of the information that comes up in the tuple?

Any help is apprecieated. Thanks.

Was it helpful?

Solution

You can extract some of the data with comprehensions, like this:

[d[k]['a']['n'] for k in d]
# => [u'Backups', 'Rubbish Bin', u'Test3', u'Abandon All Ships - 5 - Stange Love.mp3']

(assuming you have your dict (and it's a dict, as Dan and David say, not a tuple) in d).

OTHER TIPS

What you appear to have showed is not in fact a tuple but a dictionary which is made of of key/value pairs, and some of those values are themselves dictionaries with key/value pairs some of which contain tuples.

If this Dict is called MusicDict for example you would access the information you need like this:

To access the first line information:

MusicDict['0UsShTrY']['p']

This will return '5RtyGQwS'

To access Access all ships:

MusicDict['kEk0RbKR'][['a']['n']

This will return "Abandon All Ships"

I'm not sure this is the most helpful structure for your data though, it looks a little confused.

Your data is stored in nested dictionaries, not tuples. In a dictionary, you can access a value directly using its key (e.g. d['key']). In the case of tuples, you can only access an element by its index (e.g. t[2] would access the 3rd element in tuple t).

To access the value "Test3" from your data, you can do it by

data['AclTQTAa']['a']['n']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top