6

I have the following sample data as a listobject in python.

[{'itemdef': 10541,
    'description': 'Dota 2 Just For Fun tournament. ', 
    'tournament_url': 'https://binarybeast.com/xDOTA21404228/', 
    'leagueid': 1212, 
    'name': 'Dota 2 Just For Fun'}, 
{'itemdef': 10742, 
    'description': 'The global Dota 2 league for everyone.', 
    'tournament_url': 'http://www.joindota.com/en/leagues/', 
    'leagueid': 1640, 
    'name': 'joinDOTA League Season 3'}]

How can i remove the description, tour_url from this list; or how can I only keep the name and leagueid keys. I have tried a variety of solutions, but it just doesnt seem to work.

2nd question: how can I filter this list? As in mysql:

select *
from table
where leagueid = 1212

Please treat me like a new person to python, because i really am.

Adam
  • 171
  • 2
  • 10

3 Answers3

10

In fact list does not have keys, list has indices, and dictionary has keys. In your case, you have a list of dictionaries, and what you need is to remove some keys (2 exactly: description and tournament_url) form each item (dictionary) of your list:

for item in my_list:  # my_list if the list that you have in your question
    del item['description']
    del item['tournament_url']

To retrieve an item from your above list with some criteria, you can do:

[item for item in my_list if your_condition_here]

Example:

>>> [item for item in my_list if item['itemdef'] == 10541]
[{'leagueid': 1212, 'itemdef': 10541, 'name': 'Dota 2 Just For Fun'}]

Edit:

To filter my_list items to retrieve only some keys, you can do:

keys_to_keep = ['itemdef', 'name']

res = [{ key: item[key] for key in keys_to_keep } for item in my_list]
print(res)
# Output: [{'itemdef': 10541, 'name': 'Dota 2 Just For Fun'}, {'itemdef': 10742, 'name': 'joinDOTA League Season 3'}]
ettanany
  • 19,038
  • 9
  • 47
  • 63
  • is there a difference between `del` and `item.pop`? – rassar Dec 01 '16 at 16:56
  • Wait, so I have a list of dictionaries? Damn, thanks a lot. I have been stuck on this shit forever. – Adam Dec 01 '16 at 16:58
  • @rassar `item.pop('key')` will also return value of the key so it's used when we need to read and delete object. – Pavel Dec 01 '16 at 17:00
  • @rassar this answers your question http://stackoverflow.com/a/5713303/4575071 – ettanany Dec 01 '16 at 17:01
  • @ettanany is there also a way to not remove it from the current list, but select which keys i want to keep, eg: i have a list with dictionaries that contain a variable amount of keys. `del item` wont do the trick here (or if you could perheps invert it. Can you help me out? – Adam Dec 01 '16 at 22:56
  • @Adam take a look at my edited answer, I added what you are looking for. – ettanany Dec 01 '16 at 23:10
  • Im so thankful for your answer. Do the keys of `keys_to_keep` stay in that order? Or does it keep changing its position everytime i rerun the code? – Adam Dec 01 '16 at 23:27
  • `keys_to_keep` is just a list of keys that you want to retrieve, the order is not important. – ettanany Dec 01 '16 at 23:28
  • Uhm, i meant the order of keys in `res` ofcourse, my bad. – Adam Dec 01 '16 at 23:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129586/discussion-between-ettanany-and-adam). – ettanany Dec 01 '16 at 23:33
0

For the first question:

for item in table:
    item.pop(tournament_url)

For the second question:

[item for item in table if item[leagueid] == 1212]
rassar
  • 5,412
  • 3
  • 25
  • 41
-1

So what you have there is a list of dictionaries. To remove the tournament_url key from the dictionaries, we'll use a dictionary comprehension

my_list = [{k:v for k, v in d.items() if k != 'tournament_url'} for d in my_list]

Read more about comprehensions in the official python documentation

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • 1
    This will recreate the list and all of the dictionaries. It's easier and quicker to iterate over the list removing the keys that aren't needed. – Holloway Dec 01 '16 at 16:55
  • print [{key: value for key, value in d[x].items() if key!='tournament_url' and key!='description'} for x in range(len(d))] – petruz Dec 01 '16 at 17:28