Solved! Go to Solution.
This is not a list, but a tuple, and a strange one at that. But that's not unusual, you can run into strange Python data structures from time to time. This one is a tuple with a dictionary as the 2nd item. The dictionary keys are strings, and the values are single-item lists. Strange indeed. Looks like maybe some xml converted to json or something. Anyway, here's how to get the emp number out of it:t = ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}) emp = t[1]['emp'][0]
Now, if you really do have a list of these tuples, then you can search for a particular emp number with a list comprehension like so:lst = [ ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}), ('id=495, Country=UK', {'emp': ['8888'], 'Code': ['09800']}), ] lst2 = [i for i in lst if i[1]['emp'][0] == '9999']
This will result in a list (lst2) of all tuples where the 'emp' key in the dictionary is a list with the single value of '9999'. If lst2 ends up empty, then no match was found.
hope this helps,
Mike
t = ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}) emp = t[1]['emp'][0]
lst = [ ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}), ('id=495, Country=UK', {'emp': ['8888'], 'Code': ['09800']}), ] lst2 = [i for i in lst if i[1]['emp'][0] == '9999']
This is not a list, but a tuple, and a strange one at that. But that's not unusual, you can run into strange Python data structures from time to time. This one is a tuple with a dictionary as the 2nd item. The dictionary keys are strings, and the values are single-item lists. Strange indeed. Looks like maybe some xml converted to json or something. Anyway, here's how to get the emp number out of it:t = ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}) emp = t[1]['emp'][0]
Now, if you really do have a list of these tuples, then you can search for a particular emp number with a list comprehension like so:lst = [ ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}), ('id=495, Country=UK', {'emp': ['8888'], 'Code': ['09800']}), ] lst2 = [i for i in lst if i[1]['emp'][0] == '9999']
This will result in a list (lst2) of all tuples where the 'emp' key in the dictionary is a list with the single value of '9999'. If lst2 ends up empty, then no match was found.
hope this helps,
Mike
lst = [ ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}), ('id=495, Country=UK', {'emp': ['8888'], 'Code': ['09800']}), ] for t in lst: d = t[1] emp = d['emp'][0] #do whatever you need to here to get the info for the next steps d['Num'] = ['004'] d['LocCity'] = ['Notts'] #etc
To add the keys to the dictionary (2nd item of each tuple), try something like this:lst = [ ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}), ('id=495, Country=UK', {'emp': ['8888'], 'Code': ['09800']}), ] for t in lst: d = t[1] emp = d['emp'][0] #do whatever you need to here to get the info for the next steps d['Num'] = ['004'] d['LocCity'] = ['Notts'] #etc
A tuple is immutable, so you won't be able to add a new dict (unless you convert each to a list), but you can add key:value pairs to the existing dict. One thing that bothers me is that all the values in the dict are single item lists. Why not just make them strings and loose the lists? I can think of at least 1 reason why you would need this structure (storing multiple values), but if that is not the case, then this structure just makes things more difficult to get at.
good luck
Mike