<?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 Bassel Haidar on Medium]]></title>
        <description><![CDATA[Stories by Bassel Haidar on Medium]]></description>
        <link>https://medium.com/@bhaidar?source=rss-91387b8b87c2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*17bT2yrqfFUOIqBo27xoxw.png</url>
            <title>Stories by Bassel Haidar on Medium</title>
            <link>https://medium.com/@bhaidar?source=rss-91387b8b87c2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 23:13:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bhaidar/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[Sorting in Python]]></title>
            <link>https://medium.com/swlh/sorting-in-python-62f6d88ecf2e?source=rss-91387b8b87c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/62f6d88ecf2e</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[sorting]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Bassel Haidar]]></dc:creator>
            <pubDate>Sun, 02 Jun 2019 14:46:01 GMT</pubDate>
            <atom:updated>2019-06-03T12:12:19.756Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ETif-MdWNPUQNWiR" /><figcaption>Photo by <a href="https://unsplash.com/@imaxpanama?utm_source=medium&amp;utm_medium=referral">Max Panamá</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Sorting is a common activity in the life of software developers . In a standard Python installation, developers have two options to sort their data, either by using the function sorted() or the method sort().</p><p>On the surface they seem to be quite similar but upon closer inspection there are some stark differences in terms of the data types they can be used on, the return value they produce, and whether the sorted data is permanently modified or not. Additionally, for the uninitiated, they may produce some unexpected results.</p><h4>Let’s take a look at sorted() through a series of code examples using numerical data first.</h4><pre># sorts a list of integers<br>numbers = [56, 90, 31, 1, 17, 100]<br>sorted(numbers)</pre><pre>[1, 17, 31, 56, 90, 100]</pre><pre># notice when numbers is called after being sorted, it returns the  # list of integers in their original order <br>numbers<br>[56, 90, 31, 1, 17, 100]</pre><p>Sorted() is a built-in function. It provides an ordered list as a return value. The sorting is in ascending order by default, and the sorted data is not saved meaning the sorting is not in-place.</p><pre># to keep the list in sorted order, assign the sorted return value  # to numbers_sorted<br>numbers_sorted = sorted(numbers) <br>numbers_sorted<br>[1, 17, 31, 56, 90, 100]</pre><pre># similarly, sorted() can be used on tuples and sets and its return # value is assigned to numbers_tuple_sorted and numbers_set_sorted<br>numbers_tuple = (56, 90, 31, 1, 17, 100)<br>numbers_set = {50, 12, 12, 9, 7, 0}<br>numbers_tuple_sorted = sorted(numbers_tuple)<br>numbers_set_sorted = sorted(numbers_set)</pre><p>Numbers_tuple_sorted and numbers_set_sorted are of type list because sorted() returns an ordered list.</p><pre>numbers_tuple_sorted<br>[1, 17, 31, 56, 90, 100]</pre><pre>type(numbers_tuple_sorted)<br>list</pre><pre># by definition a set is an unordered collection data type that is  # iterable, mutable, and has no duplicate elements - 12 appears once<br>numbers_set_sorted<br>[0, 7, 9, 12, 50]</pre><pre>type(numbers_set_sorted)<br>list</pre><p>The returned list object can be cast to another data type.</p><pre># when casting numbers_tuple_sorted to a tuple, it remains sorted<br>tuple(numbers_tuple_sorted)<br>(1, 17, 31, 56, 90, 100)</pre><pre># when casting numbers_set_sorted to a set, we lose the sorting     # because a set by definition is an unordered collection<br>set(numbers_set_sorted)<br>{0, 7, 9, 12, 50}</pre><h4>Sorted() with strings</h4><p>Sorted() treats a string like a list. It iterates over each element, sorts each character, including spaces.</p><pre>string_number = ‘78932’ <br>string_value = ‘Sorting is fun’<br>sorted_string_number = sorted(string_number)<br>sorted_string = sorted(string_value)<br>sorted_string_number<br>[&#39;2&#39;, &#39;3&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;]</pre><pre>sorted_string<br>[&#39; &#39;, &#39; &#39;, &#39;S&#39;, &#39;f&#39;, &#39;g&#39;, &#39;i&#39;, &#39;i&#39;, &#39;n&#39;, &#39;n&#39;, &#39;o&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;]</pre><pre># using split() to clean up the output<br>sorted_string = sorted(string_value.split())<br>sorted_string</pre><pre>[&#39;Sorting&#39;, &#39;fun&#39;, &#39;is&#39;]</pre><pre># put the sorted string together with join()<br>‘ ‘.join(sorted_string)</pre><pre>&#39;Sorting fun is&#39;</pre><p>Lists with non-comparable data types can’t be sorted.</p><pre>mixed_numbers = [201, “10”, 789, “31”, 40, 0]<br>sorted(mixed_numbers)<br>--------------------------------------------------------------------<br>TypeError                                 Traceback (most recent call last)<br>&lt;ipython-input-68-790f19871423&gt; in &lt;module&gt;()<br><strong>      1</strong> mixed_numbers = [201, &quot;10&quot;, 789, &quot;31&quot;, 40, 0]<br>----&gt; 2 sorted(mixed_numbers)<br><br>TypeError: &#39;&lt;&#39; not supported between instances of &#39;str&#39; and &#39;int&#39;</pre><p>A combination of integers and strings that are all numbers can be cast to comparable data types by using a list comprehension.</p><pre># Each element in mixed_numbers has int() called on it to convert   # its str values to int values, allowing us to compare each element # and provide a sorted output without raising an error<br>sorted([int(x) for x in mixed_numbers])<br>[0, 10, 31, 40, 201, 789]</pre><p>Evaluations are implicitly converted by Python to boolean data type.</p><pre># The 1 was moved to the last position in the list, all other values # are equal so they retain their original order relative to each    # other<br>similar_values = [False, 0, 1, ‘x’ == ‘B’, 18 &lt;= 0]<br>sorted(similar_values)<br>[0, False, False, True, 1]</pre><pre># notice that 1 == 2 is converted to False and all the values are       # considered equal and retained their original positions<br>false_values = [False, 0, 0, 1 == 2, 0, False, False]<br>sorted(false_values)</pre><pre>[False, 0, 0, False, 0, False, False]</pre><p>When sorting strings, case matters. Sorted() can be used on a list of strings to sort the values in ascending order, which appears alphabetically by default.</p><pre>names = [‘Brahm’, ‘Helen’, ‘Omer’, ‘Manisha’, ‘Emefa’, ‘Angel’, ‘Nick’]<br>sorted(names)</pre><pre>[&#39;Angel&#39;, &#39;Brahm&#39;, &#39;Emefa&#39;, &#39;Helen&#39;, &#39;Manisha&#39;, &#39;Nick&#39;, &#39;Omer&#39;]</pre><p>Using the built_in function <a href="https://docs.python.org/3/library/functions.html#ord">ord()</a>, we can get the integer value of the <a href="https://docs.python.org/3/howto/unicode.html">Unicode</a> code point of a character. If the first letter is the same, then sorted() will use the second character to determine the order all the way to the end of the string.</p><pre>names_with_case = [‘Brahm’, ‘helen’, ‘Omer’, ‘Manisha’, ‘Emefa’, ‘angel’, ‘nick’]<br>sorted(names_with_case)</pre><pre>[&#39;Brahm&#39;, &#39;Emefa&#39;, &#39;Manisha&#39;, &#39;Omer&#39;, &#39;angel&#39;, &#39;helen&#39;, &#39;nick&#39;]</pre><pre># List comprehension to get the Unicode value the first letter in   # each name<br>[(ord(name[0]), name[0]) for name in sorted(names_with_case)]</pre><pre>[(66, &#39;B&#39;), (69, &#39;E&#39;), (77, &#39;M&#39;), (79, &#39;O&#39;), (97, &#39;a&#39;), (104, &#39;h&#39;), (110, &#39;n&#39;)]</pre><p>Using sorted() with reverse=True changes the default sorting to descending order.</p><pre>names = [‘Brahm’, ‘Helen’, ‘Omer’, ‘Manisha’, ‘Emefa’, ‘Angel’]<br>sorted(names, reverse=True)<br>[&#39;Omer&#39;, &#39;Manisha&#39;, &#39;Helen&#39;, &#39;Emefa&#39;, &#39;Brahm&#39;, &#39;Angel&#39;]</pre><blockquote><strong>What makes sorted() so versatile and flexible is the keyword parameter called key</strong></blockquote><p>Key expects a function to be passed to it as an argument. The result of the function will ultimately determine the sort order of the object that sorted() is used on.</p><p>As mentioned earlier, Python uses a Unicode code points value, meaning every character is mapped to a numeric value (i.e. ‘A’ =65, ‘B’=66, ‘a’=97, ‘b’=98, and ‘ ’ = 32). Notice that capital letters have a lower value than lower case letters. To sort a mixed case strings in alphabetical order, we apply the built-in method str.lower (we could have also used str.upper).</p><pre>names_with_case = [‘Brahm’, ‘helen’, ‘Omer’, ‘Manisha’, ‘Emefa’, ‘angel’, ‘nick’]<br>sorted(names_with_case, key=str.lower)</pre><pre>[&#39;angel&#39;, &#39;Brahm&#39;, &#39;Emefa&#39;, &#39;helen&#39;, &#39;Manisha&#39;, &#39;nick&#39;, &#39;Omer&#39;]</pre><p>The argument key in sorted() is extremely powerful because almost any function, built-in or user-defined, can be used to manipulate the output order. In the employee_salary_tuple below, we have a list of tuples containing employee names and their corresponding salaries. We used a lambda function to sort them by salary.</p><pre># lists are 0 based index in Python, that&#39;s why when we use the     # lambda function to slice out the salary we access it using index 1 <br>employee_salary_tuple = [(‘Angel’, 120000), (‘Joe’, 80000), (‘Jim’, 40000)]<br>sorted(employee_salary_tuple, key=lambda employee: employee[1])</pre><pre>[(&#39;Jim&#39;, 40000), (&#39;Joe&#39;, 80000), (&#39;Angel&#39;, 120000)]</pre><p>Suppose we need to sort strings in reverse order. We can accomplish this by creating a function that reverses the word and then use this function as an argument for key in sorted() as shown below.</p><pre>def reverse_word(word):<br>    return word[::-1] # return word in reverse order</pre><pre>words = [&#39;Worldly&#39;, &#39;wiley&#39;, &#39;emotionally&#39;, &#39;Aztec&#39;]<br>sorted(words, key=reverse_word)</pre><pre>[&#39;Aztec&#39;, &#39;wiley&#39;, &#39;Worldly&#39;, &#39;emotionally&#39;]</pre><p>Instead of writing a standalone function, we can also use a lambda function.</p><pre>words = [‘Worldly’, ‘wiley’, ‘emotionally’, ‘Aztec’]<br>sorted(words, key=lambda x: x[::-1])</pre><pre>[&#39;Aztec&#39;, &#39;wiley&#39;, &#39;Worldly&#39;, &#39;emotionally&#39;]</pre><h4><strong>Now, let’s investigate sort()</strong></h4><p>Sort() is a method of the list class and can only be used on lists.</p><pre># calling .sort() like sorted() - calling it like a function instead # of the dot notation for a method will raise an error<br>values_to_sort = [56, 90, 31, 1, 17, 100]<br>sort(values_to_sort)</pre><pre>--------------------------------------------------------------------<br>NameError                                 Traceback (most recent call last)<br>&lt;ipython-input-94-900da950d474&gt; in &lt;module&gt;()<br><strong>      1</strong> values_to_sort = [56, 90, 31, 1, 17, 100]<br><strong>      2</strong> # Try to call .sort() like sorted()<br>----&gt; 3 sort(values_to_sort)<br><br>NameError: name &#39;sort&#39; is not defined</pre><pre># using sort() on a tuple will also raise an error - it can only be # used on a list</pre><pre>tuple_val = (56, 90, 31, 1, 17, 100)<br>tuple_val.sort()</pre><pre>--------------------------------------------------------------------<br>AttributeError                            Traceback (most recent call last)<br>&lt;ipython-input-95-3db46faa9171&gt; in &lt;module&gt;()<br><strong>      1</strong> # Try to use .sort() on a tuple<br><strong>      2</strong> tuple_val = (56, 90, 31, 1, 17, 100)<br>----&gt; 3 tuple_val.sort()<br><br>AttributeError: &#39;tuple&#39; object has no attribute &#39;sort&#39;</pre><p>There is no ordered output in sort(), so the assignment to a new variable only passes a None type.</p><pre># Sort the list and assign to new variable<br>sorted_values = values_to_sort.sort()<br>print(sorted_values)</pre><pre>None</pre><p>Sort() does the sorting in-place. If we print values_to_sort, we see that it has changed and its original order was not maintained.</p><pre>print(values_to_sort)<br>[1, 17, 31, 56, 90, 100]</pre><p>The same key and reverse arguments in sorted() are also available in sort().</p><p>In summary, sort() works only on a list data type, modifies the list it is called on in-place, and has no return value. Sorted() works on a list, tuple, or set. It returns a list that can be assigned to a variable, and does not modify the original list in-place.</p><p>References:</p><p>Fundakowski, D. (2019, April 23). How to Use sorted() and sort() in Python — Real Python. Retrieved from <a href="https://realpython.com/python-sort/">https://realpython.com/python-sort/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62f6d88ecf2e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/sorting-in-python-62f6d88ecf2e">Sorting in Python</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why civil war and immigration can make you a resilient data scientist]]></title>
            <link>https://medium.com/future-vision/why-civil-war-and-immigration-can-make-you-a-resilient-data-scientist-1f388ccb96c?source=rss-91387b8b87c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f388ccb96c</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[big-data]]></category>
            <dc:creator><![CDATA[Bassel Haidar]]></dc:creator>
            <pubDate>Fri, 10 May 2019 12:01:02 GMT</pubDate>
            <atom:updated>2019-06-03T15:44:39.498Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Why civil war and an immigrant mindset can make you a resilient data scientist</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RS5wqrfUDoxFyjze" /><figcaption>Photo by <a href="https://unsplash.com/@stijnswinnen?utm_source=medium&amp;utm_medium=referral">Stijn Swinnen</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In life, we don’t get any do-overs, but we can certainly start over.</p><blockquote>Getting comfortable with being uncomfortable is what starting over is all about.</blockquote><p>Growing up in a civil war was no picnic, but as many of my Lebanese compatriots can attest, it also strengthens your resolve, fortifies your<em> </em>grit, and boosts your resiliency. I have been part of successful businesses and part of ones that have failed miserably, but neither success nor failure is constant.</p><blockquote>The only constant is change. And our ability to rebound from challenging situations is directly proportional to how much resolve, grit, and resiliency we have accumulated along the way.</blockquote><p>For the past couple of decades, I worked as a C-level executive for technology companies in the Government contracting space and the health and wellness industry. In the past year, I got the urge to get back to my technical roots (I have BS and MS degrees in Computer Science). I learned Python, dabbled with AWS’s dizzying array of cloud services with an emphasis on machine learning, and built a machine learning classifier through <a href="https://www.udacity.com/course/ai-programming-python-nanodegree--nd089">Udacity’s nanodegree ‘Artificial Intelligence Programming with Python</a>’.</p><p>I was recently accepted into the <a href="https://flatironschool.com/fellowships/data-science-bootcamp/dc/">Flatiron School Data Science Fellowship</a> in Washington, DC. It is a 15-week full-time immersive program that covers exploratory data analysis, descriptive statistics, advanced data retrieval, statistical analysis, models of machine learning, big data, deep learning, and other advanced topics. Some of my classmates have Ph.D.’s in astrophysics, philosophy, engineering, public administration as well as medical doctors, while others have master’s degrees in mathematics, statistics, physics, economics and computer science.</p><p>The program is intense — it is a boot camp — but going through it with this caliber of talent is transformative. It has only been 3 weeks since I started my fellowship and I cannot believe how much I learned already.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WDlD5SRlGS_WFREC" /><figcaption>Photo by <a href="https://unsplash.com/@ffstop?utm_source=medium&amp;utm_medium=referral">Fotis Fotopoulos</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote>Numpy, pandas, scipy, matplotlib, seaborn, and sklearn (some of Python’s libraries) are not for the faint of heart, but that sniper on my street prepared me for this challenge. Statistical models are a beast, but they are a breeze compared to living in three continents and immigrating 26 years ago to my new home. I have seen more conflicts with git than the entire Lebanese war :)</blockquote><p>Data that is cleansed and harnessed holds so much power. It can be a catalyst for change as it fosters new insights and promotes new levels of understanding. We live in an era that is ushering in an unprecedented advancement in technological breakthroughs and creating along the way new types of jobs that require a unique blend of skill-set (communication, presentation, analysis, domain expertise, programming, AI/ML, analytics, business intelligence, linear algebra, statistics, and calculus).</p><blockquote>According to an article published by Bernard Marr in Forbes Magazine, “over 90% of all the world data has been produced in the last two years alone and each day we produce 2.5 quintillion bytes of data (a quintillion is a number with fifteen zeros), but that pace is only accelerating with the growth of the Internet of Things (IoT)!</blockquote><p>I want to be part of this new army of data scientists who are revolutionizing every field in every aspect of life. “I want to put a ding in the universe,” said the late Steve Jobs, and I, too along with my fellow data scientist are arming ourselves with the necessary skill-set to decode this daily tsunami of data using science to put our ding in the universe.</p><p><strong>Sources</strong>: <br>Marr, Bernard. “How Much Data Do We Create Every Day? The Mind-Blowing Stats Everyone Should Read.” <em>Forbes</em>, Forbes Magazine, 11 Mar. 2019, <a href="http://www.forbes.com/sites/bernardmarr/2018/05/21/how-much-data-do-we-create-every-day-the-mind-blowing-stats-everyone-should-read/#3dd740e360ba.">www.forbes.com/sites/bernardmarr/2018/05/21/how-much-data-do-we-create-every-day-the-mind-blowing-stats-everyone-should-read/#3dd740e360ba.</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F55d9bb-2%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2F55d9bb-2%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/86ec3ea532fbe52237a7437f4e1a4707/href">https://medium.com/media/86ec3ea532fbe52237a7437f4e1a4707/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f388ccb96c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/future-vision/why-civil-war-and-immigration-can-make-you-a-resilient-data-scientist-1f388ccb96c">Why civil war and immigration can make you a resilient data scientist</a> was originally published in <a href="https://medium.com/future-vision">Future Vision</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>