Select to view content in your preferred language

Searching a LIST in Python

832
5
Jump to solution
11-10-2012 10:35 AM
BarbBen
Emerging Contributor
Hi, I have a list which looks like ('id=495, Country=UK', {'emp': ['9999'], 'Code': ['09800']}) and I need to check the value of emp number from this list. How do I do that? Basically checking if some other variable is equal to value of employee Id? Thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BarbBen
Emerging Contributor
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


Thanks Mike Really appreciate it...Will try this code.....TQ.

View solution in original post

0 Kudos
5 Replies
MikeHunter
Frequent Contributor
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
0 Kudos
BarbBen
Emerging Contributor
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


Thanks Mike Really appreciate it...Will try this code.....TQ.
0 Kudos
BarbBen
Emerging Contributor
Now I need to expand this structure to
[('ID1=1,c=UK',
       {'Num': ['004'], 'locCity': 'Notts'], 'name': ['Roger'],'uid': [lname.fname@email.com],'Date': [101011]}),
('key1=2,c=UK',
       {'Num': ['005'], 'locCity': ['Birmi'], 'name': ['Din'],'uid':[lname1.fname1@email.com],'Date': [101111]}),
('key1=3,c=UK',
      {'Num': ['006'], 'locCity': ['Clift'], 'name': ['Dan'],'uid':[lname2.fname2@email.com],'Date': [111011]})]

I have a string with lastname.firstname. With this I should loop thru the above tuples and for the matching entry, I have to store keys like Num, locCity and Date and their values. So I believe it should be a dictionary? so I can use these 3 values later....
Could it be done? Please advise.
0 Kudos
MikeHunter
Frequent Contributor
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
0 Kudos
BarbBen
Emerging Contributor
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


Thanks Mike for the reply. Apologies for confusing you. I meant to say I have a new LIST 
[('key1=1,c=UK',
      {'Num': ['004'], 'locCity': 'Notts'], 'name': ['Roger'],'uid': [lname.fname@email.com],'Date': [101011]}),
('key1=2,c=UK',
      {'Num': ['005'], 'locCity': ['Birmi'], 'name': ['Din'],'uid':[lname1.fname1@email.com],'Date': [101111]}),
('key1=3,c=UK',
      {'Num': ['006'], 'locCity': ['Clift'], 'name': ['Dan'],'uid':[lname2.fname2@email.com],'Date': [111011]})]

I have a string with lastname.firstname. With this I should loop thru the above tuples and for the matching entry, I have to store keys like Num, locCity and Date and their values. So I believe it should be a dictionary? so I can use these 3 values later....
Could it be done? Please advise.  Like I need to get the field and its value.
My list will have around 50 fields in its t[1] and I need to pick few from them(2 or 3) with name, value pairs so I can get those values later.
0 Kudos