<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aayushi Agarwal on Medium]]></title>
        <description><![CDATA[Stories by Aayushi Agarwal on Medium]]></description>
        <link>https://medium.com/@aayushiagarwal1999?source=rss-bec32adbe7b2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Aayushi Agarwal on Medium</title>
            <link>https://medium.com/@aayushiagarwal1999?source=rss-bec32adbe7b2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 05:51:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aayushiagarwal1999/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Python: Grouping & sorting list of dictionaries]]></title>
            <link>https://medium.com/@aayushiagarwal1999/python-grouping-sorting-list-of-dictionaries-3a82747fb09c?source=rss-bec32adbe7b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a82747fb09c</guid>
            <category><![CDATA[list-of-python-dictionary]]></category>
            <category><![CDATA[python-dictionaries]]></category>
            <category><![CDATA[python-sorting]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Aayushi Agarwal]]></dc:creator>
            <pubDate>Thu, 13 Jun 2024 03:54:55 GMT</pubDate>
            <atom:updated>2024-06-13T04:20:03.144Z</atom:updated>
            <content:encoded><![CDATA[<p>Suppose we have a list of dictionaries of Student names, Major subjects and Grades scored</p><pre># list of students with their Major &amp; Scores<br>students = [<br>    {&quot;Name&quot;: &quot;ABC&quot;, &quot;Major&quot;: &quot;Engineering&quot;, &quot;Grade&quot;: &quot;A&quot;},<br>    {&quot;Name&quot;: &quot;KLM&quot;, &quot;Major&quot;: &quot;Agriculture&quot;, &quot;Grade&quot;: &quot;B&quot;},<br>    {&quot;Name&quot;: &quot;PQR&quot;, &quot;Major&quot;: &quot;Engineering&quot;, &quot;Grade&quot;: &quot;B&quot;},<br>    {&quot;Name&quot;: &quot;XYZ&quot;, &quot;Major&quot;: &quot;Engineering&quot;, &quot;Grade&quot;: &quot;C&quot;},<br>]</pre><p>Now, we need to sort the data by “Major” subject:</p><pre>from operator import itemgetter<br><br># sort students by &quot;Major&quot; key<br>students = sorted(students, key = itemgetter(&#39;Major&#39;))</pre><p>The above uses a built-in <em>sorted()</em> method to sort list of dictionaries. We specify the sorting condition in the sort method’s <em>key </em>argument.<br>The <em>operator.itemgetter()</em> returns a callable object that fetches an item from an object.</p><p>Output of above is:</p><pre>[<br>{&#39;Name&#39;: &#39;KLM&#39;, &#39;Major&#39;: &#39;Agriculture&#39;, &#39;Grade&#39;: &#39;B&#39;},<br>{&#39;Name&#39;: &#39;ABC&#39;, &#39;Major&#39;: &#39;Engineering&#39;, &#39;Grade&#39;: &#39;A&#39;},<br>{&#39;Name&#39;: &#39;PQR&#39;, &#39;Major&#39;: &#39;Engineering&#39;, &#39;Grade&#39;: &#39;B&#39;},<br>{&#39;Name&#39;: &#39;XYZ&#39;, &#39;Major&#39;: &#39;Engineering&#39;, &#39;Grade&#39;: &#39;C&#39;}<br>]</pre><p>Also, let’s group the data by Major subject, and then sort by Grade scored:</p><pre>from itertools import groupby<br>from operator import itemgetter<br><br># Display data grouped by Major Subject<br>for key, value in groupby(students, key = itemgetter(&#39;Major&#39;)):<br>    # Display data sorted by Grade in each Major<br>    print(key, sorted(value, key = itemgetter(&#39;Grade&#39;)))</pre><p><em>itertools.groupby(iterable, key=None)</em>: Make an iterator that returns consecutive keys and groups from the <em>iterable</em>.<br>It generates a break or new group every time the value of the key function changes (which is why it is usually <strong>necessary </strong>to have sorted the data using the same key function).</p><p>Here, output of above is:</p><pre>Agriculture [{&#39;Name&#39;: &#39;KLM&#39;, &#39;Major&#39;: &#39;Agriculture&#39;, &#39;Grade&#39;: &#39;B&#39;}]<br><br>Engineering [{&#39;Name&#39;: &#39;ABC&#39;, &#39;Major&#39;: &#39;Engineering&#39;, &#39;Grade&#39;: &#39;A&#39;}, <br>             {&#39;Name&#39;: &#39;PQR&#39;, &#39;Major&#39;: &#39;Engineering&#39;, &#39;Grade&#39;: &#39;B&#39;}, <br>             {&#39;Name&#39;: &#39;XYZ&#39;, &#39;Major&#39;: &#39;Engineering&#39;, &#39;Grade&#39;: &#39;C&#39;}<br>             ]</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a82747fb09c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>