[Tut] How to Filter a List of Dictionaries in Python? - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Filter a List of Dictionaries in Python? (/thread-94565.html) |
[Tut] How to Filter a List of Dictionaries in Python? - xSicKxBot - 04-15-2020 How to Filter a List of Dictionaries in Python? <div><p>In this article, you’ll learn the ins and outs of the sorting function in Python. In particular, you’re going to learn <strong>how to filter a list of dictionaries</strong>. So let’s get started!</p> <p><strong>Short answer: The list comprehension statement <code>[x for x in lst if condition(x)]</code> creates a new list of dictionaries that meet the condition. All dictionaries in <code>lst</code> that don’t meet the condition are filtered out. You can define your own condition on list element <code>x</code>.</strong></p> <p>Here’s a quick and minimal example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">l = [{'key':10}, {'key':4}, {'key':8}] def condition(dic): ''' Define your own condition here''' return dic['key'] > 7 filtered = [d for d in l if condition(d)] print(filtered) # [{'key': 10}, {'key': 8}]</pre> <p>Try it yourself in the interactive Python shell (in your browser):</p> <p> <iframe height="600px" width="100%" src="https://repl.it/@finxter/filterlistdict3?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p> <p>You’ll now get the step-by-step solution of this solution. I tried to keep it as simple as possible. So keep reading!</p> <h2>Filter a List of Dictionaries By Value</h2> <p><strong>Problem</strong>: Given a list of dictionaries. Each dictionary consists of one or more (key, value) pairs. You want to filter them by value of a particular dictionary key (<em>attribute</em>). How do you do this?</p> <p><strong>Minimal Example</strong>: Consider the following example where you’ve three user dictionaries with <code>username</code>, <code>age</code>, and <code>play_time</code> keys. You want to get a list of all users that meet a certain condition such as <code>play_time>100</code>. Here’s what you try to accomplish:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">users = [{'username': 'alice', 'age': 23, 'play_time': 101}, {'username': 'bob', 'age': 31, 'play_time': 88}, {'username': 'ann', 'age': 25, 'play_time': 121},] superplayers = # Filtering Magic Here print(superplayers)</pre> <p>The output should look like this where the <code>play_time</code> attribute determines whether a dictionary passes the filter or not, i.e., <code>play_time>100</code>:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[{'username': 'alice', 'age': 23, 'play_time': 101}, {'username': 'ann', 'age': 25, 'play_time': 121}]</pre> <p><strong>Solution</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-get-rid-of-pythons-map-function-with-list-comprehension/" target="_blank">list comprehension</a> <code>[x for x in lst if condition(x)]</code> to create a new list of dictionaries that meet the condition. All dictionaries in <code>lst</code> that don’t meet the condition are filtered out. You can define your own condition on list element <code>x</code>. </p> <p>Here’s the code that shows you how to filter out all user dictionaries that don’t meet the condition of having played at least 100 hours.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">users = [{'username': 'alice', 'age': 23, 'play_time': 101}, {'username': 'bob', 'age': 31, 'play_time': 88}, {'username': 'ann', 'age': 25, 'play_time': 121},] superplayers = [user for user in users if user['play_time']>100] print(superplayers)</pre> <p>The output is the filtered list of dictionaries that meet the condition:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[{'username': 'alice', 'age': 23, 'play_time': 101}, {'username': 'ann', 'age': 25, 'play_time': 121}]</pre> <p><strong>Try It Yourself:</strong></p> <p> <iframe height="600px" width="100%" src="https://repl.it/@finxter/filterlistdictionaries?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p> <p><strong>Related articles on the Finxter blog:</strong></p> <ul> <li><a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehensio</a><a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener">n</a></li> <li><a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank">Lambda Functions</a></li> <li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionaries</a></li> </ul> <h2>Filter a List of Dictionaries By Key</h2> <p><strong>Problem</strong>: Given a list of dictionaries. Each dictionary consists of one or more (key, value) pairs. You want to filter them by key (<em>attribute</em>). All dictionaries that don’t have this key (attribute) should be filtered out. How do you do this?</p> <p><strong>Minimal Example</strong>: Consider the following example again where you’ve three user dictionaries with <code>username</code>, <code>age</code>, and <code>play_time</code> keys. You want to get a list of all users for which the key <code>play_time</code> exists. Here’s what you try to accomplish:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">users = [{'username': 'alice', 'age': 23, 'play_time': 101}, {'username': 'bob', 'age': 31, 'play_time': 88}, {'username': 'ann', 'age': 25},] superplayers = # Filtering Magic Here print(superplayers)</pre> <p>The output should look like this where the <code>play_time</code> attribute determines whether a dictionary passes the filter or not (as long as it exists, it shall pass the filter).</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[{'username': 'alice', 'age': 23, 'play_time': 101}, {'username': 'bob', 'age': 31, 'play_time': 88}]</pre> <p><strong>Solution</strong>: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-get-rid-of-pythons-map-function-with-list-comprehension/" target="_blank">list comprehension</a> <code>[x for x in lst if condition(x)]</code> to create a new list of dictionaries that meet the condition. All dictionaries in <code>lst</code> that don’t meet the condition are filtered out. You can define your own condition on list element <code>x</code>. </p> <p>Here’s the code that shows you how to filter out all user dictionaries that don’t meet the condition of having a key <code>play_time</code>.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">users = [{'username': 'alice', 'age': 23, 'play_time': 101}, {'username': 'bob', 'age': 31, 'play_time': 88}, {'username': 'ann', 'age': 25},] superplayers = [user for user in users if 'play_time' in user] print(superplayers)</pre> <p>The output is the filtered list of dictionaries that meet the condition:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[{'username': 'alice', 'age': 23, 'play_time': 101}, {'username': 'bob', 'age': 31, 'play_time': 88}]</pre> <p><strong>Try It Yourself:</strong></p> <p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/filterlistdict2?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p> <p><strong>Related articles on the Finxter blog:</strong></p> <ul> <li><a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehensio</a><a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener">n</a></li> <li><a rel="noreferrer noopener" href="https://blog.finxter.com/a-simple-introduction-of-the-lambda-function-in-python/" target="_blank">Lambda Functions</a></li> <li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" target="_blank">Dictionaries</a></li> </ul> <h2>Related Question: How to Sort a List of Dictionaries By Key Value</h2> <p><a href="https://blog.finxter.com/python-how-to-sort-a-list-of-dictionaries/" target="_blank" rel="noreferrer noopener">Original article</a></p> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <div class="ast-oembed-container"><iframe title="Python – How to Sort a List of Dictionaries?" width="1400" height="788" src="https://www.youtube.com/embed/aYYvCvMv7vQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div> </div> </figure> <p><strong>Problem</strong>: Given a list of dictionaries. Each dictionary consists of multiple (key, value) pairs. You want to sort them by value of a particular dictionary key (<em>attribute</em>). How do you sort this dictionary?</p> <p><strong>Minimal Example</strong>: Consider the following example where you want to sort a list of salary dictionaries by value of the key <code>'Alice'</code>. </p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">salaries = [{'Alice': 100000, 'Bob': 24000}, {'Alice': 121000, 'Bob': 48000}, {'Alice': 12000, 'Bob': 66000}] sorted_salaries = # ... Sorting Magic Here ... print(sorted_salaries)</pre> <p>The output should look like this where the salary of Alice determines the order of the dictionaries:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[{'Alice': 12000, 'Bob': 66000}, {'Alice': 100000, 'Bob': 24000}, {'Alice': 121000, 'Bob': 48000}]</pre> <p><strong>Solution</strong>: You have two main ways to do this—both are based on defining the key function of Python’s sorting methods. The key function maps each list element (in our case a dictionary) to a single value that can be used as the basis of comparison.</p> <ul> <li>Use a lambda function as key function to sort the list of dictionaries.</li> <li>Use the itemgetter function as key function to sort the list of dictionaries.</li> </ul> <p>Here’s the code of the first option using a lambda function that returns the value of the key <code>'Alice'</code> from each dictionary:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Create the dictionary of Bob's and Alice's salary data salaries = [{'Alice': 100000, 'Bob': 24000}, {'Alice': 121000, 'Bob': 48000}, {'Alice': 12000, 'Bob': 66000}] # Use the sorted() function with key argument to create a new dic. # Each dictionary list element is "reduced" to the value stored for key 'Alice' sorted_salaries = sorted(salaries, key=lambda d: d['Alice']) # Print everything to the shell print(sorted_salaries)</pre> <p>The output is the sorted dictionary. Note that the first dictionary has the smallest salary of Alice and the third dictionary has the largest salary of Alice.</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[{'Alice': 12000, 'Bob': 66000}, {'Alice': 100000, 'Bob': 24000}, {'Alice': 121000, 'Bob': 48000}]</pre> <p><strong>Try It Yourself:</strong></p> <p> <iframe src="https://repl.it/@finxter/dictionarylistof?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="800px" frameborder="no"></iframe> </p> <p><strong>Related articles:</strong></p> <ul> <li><a href="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener">The Ultimate Guide to Python Lists</a></li> </ul> <h2>Where to Go From Here</h2> <p>In this article, you’ve learned how to filter a list of dictionaries easily with a simple list comprehension statement. That’s far more efficient than using the <code>filter()</code> method proposed in many other blog tutorials. Guido, the creator of Python, <a href="https://blog.finxter.com/about-guidos-fate-of-reduce-in-python-3000/" target="_blank" rel="noreferrer noopener">hated </a>the <code>filter()</code> function!</p> <p>I’ve realized that professional coders tend to use dictionaries more often than beginners due to their superior understanding of the benefits of dictionaries. If you want to learn about those, check out my <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" target="_blank">in-depth tutorial of Python dictionaries</a>. </p> <p>If you want to stop learning and start earning with Python, check out my free webinar <a href="https://blog.finxter.com/webinar-freelancer/">“How to Become a Python Freelance Developer?”</a>. It’s a great way of starting your thriving coding business online. </p> <p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">“[Webinar] How to Become a Python Freelance Developer?”</a></p> </div> https://www.sickgaming.net/blog/2020/04/15/how-to-filter-a-list-of-dictionaries-in-python/ |