1

I would like to keep only name and value ( key value pair ) This is list.

[{u'domain': u'.facebook.com',
  u'expiry': None,
  u'name': u'c_user',
  u'path': u'/',
  u'secure': True,
  u'value': u'100001724251788'},
 {u'domain': u'.facebook.com',
  u'expiry': None,
  u'name': u'csm',
  u'path': u'/',
  u'secure': False,
  u'value': u'2'},
 {u'domain': u'.facebook.com',
  u'expiry': 1441116441,
  u'name': u'datr',
  u'path': u'/',
  u'secure': False,
  u'value': u'AUojUqoBUYA2wj4j04GT5XvX'},
 {u'domain': u'.facebook.com',
  u'expiry': 1441116442,
  u'name': u'lu',
  u'path': u'/',
  u'secure': False,
  u'value': u'RitwQJMNRJ8siUh_9eIj4SMw'},
 {u'domain': u'.facebook.com',
  u'expiry': None,
  u'name': u's',
  u'path': u'/',
  u'secure': True,
  u'value': u'Aa7ebY1RvmeilCX8.BSI0od'},
 {u'domain': u'.facebook.com',
  u'expiry': None,
  u'name': u'xs',
  u'path': u'/',
  u'secure': True,
  u'value': u'62%3AjNm_3ySkegf7cg%3A2%3A1378044445%3A10945'},
 {u'domain': u'.facebook.com',
  u'expiry': 1380636479,
  u'name': u'fr',
  u'path': u'/',
  u'secure': False,
  u'value': u'0aU8cfDygWXo1ETQA.AWV9BQIzKARYURFpuxUdXLoXcl8.BSI0od.j_.FIj.AWXjyq2t'},
 {u'domain': u'.facebook.com',
  u'expiry': None,
  u'name': u'sub',
  u'path': u'/',
  u'secure': False,
  u'value': u'64'},
 {u'domain': u'.facebook.com',
  u'expiry': None,
  u'name': u'p',
  u'path': u'/',
  u'secure': False,
  u'value': u'125'},
 {u'domain': u'.facebook.com',
  u'expiry': None,
  u'name': u'presence',
  u'path': u'/',
  u'secure': True,
  u'value': u'EM378046939EuserFA21B01724251788A2EstateFDsb2F0Et2F_5b_5dElm2FnullEuct2F1378043847BEtrFnullEtwF2448105081EatF1378046499377G378046939504CEchFDp_5f1B01724251788F13CC'}]
[Dbg]>>> type(cookies)
<type 'list'>
[Dbg]>>> newlist = [{'domain': i['domain'], 'name': i['name'], 'value': i['value']} for i in cookies]
[Dbg]>>> newlist
[{'domain': u'.facebook.com', 'name': u'c_user', 'value': u'100001724251788'},
 {'domain': u'.facebook.com', 'name': u'csm', 'value': u'2'},
 {'domain': u'.facebook.com',
  'name': u'datr',
  'value': u'AUojUqoBUYA2wj4j04GT5XvX'},
 {'domain': u'.facebook.com',
  'name': u'lu',
  'value': u'RitwQJMNRJ8siUh_9eIj4SMw'},
 {'domain': u'.facebook.com',
  'name': u's',
  'value': u'Aa7ebY1RvmeilCX8.BSI0od'},
 {'domain': u'.facebook.com',
  'name': u'xs',
  'value': u'62%3AjNm_3ySkegf7cg%3A2%3A1378044445%3A10945'},
 {'domain': u'.facebook.com',
  'name': u'fr',
  'value': u'0aU8cfDygWXo1ETQA.AWV9BQIzKARYURFpuxUdXLoXcl8.BSI0od.j_.FIj.AWXjyq2t'},
 {'domain': u'.facebook.com', 'name': u'sub', 'value': u'64'},
 {'domain': u'.facebook.com', 'name': u'p', 'value': u'125'},
 {'domain': u'.facebook.com',
  'name': u'presence',
  'value': u'EM378046939EuserFA21B01724251788A2EstateFDsb2F0Et2F_5b_5dElm2FnullEuct2F1378043847BEtrFnullEtwF2448105081EatF1378046499377G378046939504CEchFDp_5f1B01724251788F13CC'}]

To:

    <Cookie>.facebook.com:datr:gyenULkNKjCIJFTYDz2qbp9I
    <Cookie>.facebook.com:c_user:120004707330532
    <Cookie>.facebook.com:fr:0LglznOeWFepXcvAF.AWVm9awu51UtdvT65f9HqxYXUsI.BQp4ej._Q.AAA.AWW8GKGl
    <Cookie>.facebook.com:lu:gg1dq7zzNjC6pd7W9W0SFHnA
    <Cookie>.facebook.com:s:Aa5Zmp1zeLF36x3s
    <Cookie>.facebook.com:xs:1%3AWvWR_uXSzC3p3w%3A0%3A1374938392
    <Cookie>
4
  • But your output also has one instance of domain and expiry, not just name and value. Commented Sep 1, 2013 at 15:03
  • In your example you're also keeping the "domain" and "expiry" keys as well. What is it you're after? Commented Sep 1, 2013 at 15:04
  • 1
    Yes, i don't need domain and expiry. Commented Sep 1, 2013 at 15:04
  • @MarkR.Wilkins, thank again. What i am trying to do this. Represent my data in format ( updated just now ). But i am confused how to sort and reprent data as string like that. Commented Sep 1, 2013 at 15:39

2 Answers 2

5

To extract arbitrary names from a list of dictionaries, you could use operator.itemgetter():

from operator import itemgetter

names = ["name", "value"] # fields to extract
values = itemgetter(*names) # function that extracts values from an input dict

result = [dict(zip(names, values(d))) for d in list_of_dicts]
print(result)

Output

[{'name': u'c_user', 'value': u'100001456251788'},
 {'name': u'csm', 'value': u'2'},
 {'name': u'datr', 'value': u'AUojdfkBUYA2wj4j04GT5XvX'}]
Sign up to request clarification or add additional context in comments.

2 Comments

how to remove u' from all this?
The return value doesn't actually contain u'. That's just an indication that those are unicode strings, which you specified in your question.
3

You can use what's called a "list comprehension" to cull out the key/value pairs you want:

new_list = [{'name':x['name'], 'value':x['value']} for x in list]

A little extra explanation: What this does is loop over the entries in list, assigning each to x, then applying that value in the expression that comes at the front, in this case

{'name':x['name'], 'value':x['value']}

This can be a quick way to cull elements you want out of a more comprehensive list or to make a new list that you can define with a simple expression from the elements of an existing list.

It's also possible to do the same thing to make a new dictionary by replacing the outer square brackets [] with curly brackets {}.

Also: If you are making your new list to feed to a loop, and your original data set is quite large, you can make a "generator", which is a thing like a list that you can loop over but that doesn't store the new subset of data in memory. Making a generator replaces the square brackets [] with parentheses ().

new_generator = ({'name':x['name'], 'value':x['value']} for x in list)
for element in new_generator:
    ... do whatever ...

This avoids duplicating large amounts of data in memory and only computes each element as the loop proceeds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.