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.

0

3 Answers 3

11

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'}]
Sign up to request clarification or add additional context in comments.

10 Comments

is there a difference between del and item.pop?
Wait, so I have a list of dictionaries? Damn, thanks a lot. I have been stuck on this shit forever.
@rassar item.pop('key') will also return value of the key so it's used when we need to read and delete object.
@rassar this answers your question stackoverflow.com/a/5713303/4575071
@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?
|
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]

2 Comments

Can you explain how to use that second piece of code?
@Adam there are three equivalent variants to do this: pastebin.com/miyEgkVY
-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

2 Comments

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.
print [{key: value for key, value in d[x].items() if key!='tournament_url' and key!='description'} for x in range(len(d))]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.