<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kevin Wojkovich</title>
    <description>The latest articles on DEV Community by Kevin Wojkovich (@kwojo).</description>
    <link>https://dev.to/kwojo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F193158%2F19039f7a-2b95-48e0-8d0c-3ad5fb03637b.png</url>
      <title>DEV Community: Kevin Wojkovich</title>
      <link>https://dev.to/kwojo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kwojo"/>
    <language>en</language>
    <item>
      <title>Sorting things out in Python</title>
      <dc:creator>Kevin Wojkovich</dc:creator>
      <pubDate>Sat, 18 Jan 2020 23:25:42 +0000</pubDate>
      <link>https://dev.to/kwojo/sorting-things-out-in-python-4bn1</link>
      <guid>https://dev.to/kwojo/sorting-things-out-in-python-4bn1</guid>
      <description>&lt;p&gt;For those that know me personally, I have a weird hobby of vintage computing and have started down a rabbit hole of IBM systems.&lt;/p&gt;

&lt;p&gt;Sorting has been a task that IBM provided some solutions to help businesses process data. In the era of punch cards, one may find it necessary to sort the cards that represented program input. For example, a program that processes names may take cards as input with folks' names entered on them. For convenience these cards may need to be sorted after an army of typists cranked out decks of cards. Enter the IBM 80 series card sorting machine. &lt;strong&gt;Loudness warning:&lt;/strong&gt; see the &lt;a href="https://www.youtube.com/watch?v=jJH2alRcx4M"&gt;IBM 83&lt;/a&gt; in-action at the Computer History Museum in Mountain View California. Isn't it crazy that a refrigerator-sized machine's single function was sorting? What's more crazy is that the IBM 83 can only sort one character at a time. This means if the sorted cards had to be passed through the machine multiple times to fully sort the deck. &lt;/p&gt;

&lt;p&gt;Sorting is a classic problem in computer science. If you've taken an algorithms course, a good chunk of that course can be dedicated to sorting. I &lt;em&gt;love&lt;/em&gt; Python because the language wraps up many complex concepts into an easy-to-use experience, so you can go get work done. A reasonable sorting algorithm called &lt;a href="https://svn.python.org/projects/python/trunk/Objects/listsort.txt"&gt;Timsort&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Let's look at sorting lists in this post.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class list(object)
 |  list(iterable=(), /)
...
 |  sort(self, /, *, key=None, reverse=False)
 |      Stable sort *IN PLACE*.
 |  

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So the &lt;code&gt;list&lt;/code&gt; object has a method that affects the state of the variable. Depending on your application, this may or may not be desired.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruit = ['oranges', 'bananas', 'grapes', 'apples']
fruit.sort()
fruit
&amp;gt;&amp;gt;&amp;gt; fruit
['apples', 'bananas', 'grapes', 'oranges']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Sometimes it's necessary to preserve the original list. What do we do then?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;help(sorted)
sorted(iterable, /, *, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.

    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Coolio so we can preserve the original list using the &lt;code&gt;sorted&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruit = ['oranges', 'bananas', 'grapes', 'apples']
sorted(fruit)
&amp;gt;&amp;gt;&amp;gt; ['apples', 'bananas', 'grapes', 'oranges']
fruit
&amp;gt;&amp;gt;&amp;gt; ['oranges', 'bananas', 'grapes', 'apples']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This may be a pretty simplistic blog post. As I spend my free time playing tech archaeologist, I find it fascinating the power in higher-level languages, especially accessible languages such as Python. &lt;/p&gt;

&lt;p&gt;The takeaways I hope you walk away with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;sort()&lt;/code&gt; method on a list is &lt;em&gt;in-place&lt;/em&gt;, it changes the variable.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;sorted()&lt;/code&gt; function returns a new list.&lt;/li&gt;
&lt;li&gt;The built-in &lt;code&gt;help()&lt;/code&gt; function is a helpful reference when you need it.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Python: To append or extend</title>
      <dc:creator>Kevin Wojkovich</dc:creator>
      <pubDate>Tue, 15 Oct 2019 11:46:23 +0000</pubDate>
      <link>https://dev.to/kwojo/python-to-append-or-extend-a3f</link>
      <guid>https://dev.to/kwojo/python-to-append-or-extend-a3f</guid>
      <description>&lt;p&gt;I wrote a little tidbit this morning for my company, who is starting to roll out some Python-based services for the first time. We post little notes to help each other learn Python and to socialize the language in a shop that had only used Javascript previously.&lt;/p&gt;

&lt;p&gt;I am up early looking at some &lt;code&gt;boto3&lt;/code&gt; examples and stumbled across the &lt;code&gt;extend&lt;/code&gt; method on the Python List object. Previously I had only used the &lt;code&gt;append&lt;/code&gt; method. Also this behavior changed slightly from version 2.X to 3.7, so perhaps that's where my confusion stemmed. (Check out the version differences in the Python docs link below.)&lt;/p&gt;

&lt;p&gt;When working with lists in Python 3.7, it's pretty common to want to add more elements into that list. A common way to do this is would be to use the append method. This may not actually produce the structure you intend and using the extend method may be more intuitive.&lt;/p&gt;

&lt;p&gt;Let's say you have two lists and use the append method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = [1,2,3]
b = ['z', 'y', 'x']

a.append(b)
print(a)
# [1,2,3,['z', 'y', 'x']]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The entire list b is appended to list a.&lt;br&gt;
Let's use the extend method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a = [1,2,3]
b = ['z', 'y', 'x']

a.extend(b)
print(a)
# [1,2,3,'x','y','z']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This adds each element of the iterable list b to the end of list a.&lt;br&gt;
&lt;a href="https://docs.python.org/3.7/tutorial/datastructures.html"&gt;https://docs.python.org/3.7/tutorial/datastructures.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Apparently it is also shorthand for an assignment of slices, which is kinda cool too.&lt;/p&gt;

</description>
      <category>python</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
