<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Lists on Script Everything</title>
    <link>https://scripteverything.com/tags/lists/</link>
    <description>Recent content in Lists on Script Everything</description>
    <generator>Hugo -- 0.146.0</generator>
    <language>en-au</language>
    <lastBuildDate>Sat, 20 Jan 2024 23:14:40 +0000</lastBuildDate>
    <atom:link href="https://scripteverything.com/tags/lists/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Read A CSV File To List Of Dictionaries In Python</title>
      <link>https://scripteverything.com/read-a-csv-file-to-list-of-dictionaries-in-python/</link>
      <pubDate>Sat, 20 Jan 2024 23:14:40 +0000</pubDate>
      <guid>https://scripteverything.com/read-a-csv-file-to-list-of-dictionaries-in-python/</guid>
      <description>&lt;p&gt;The most common form of data I have played with is generally found in CSV format. This makes the data easy to understand but difficult to modify or change, especially when the CSV file contains lots of columns.&lt;/p&gt;
&lt;p&gt;In this article I will demonstrate how I frequently use this file format to import it into a useable data structure in Python, namely, a list of Python dictionaries.&lt;/p&gt;
&lt;p&gt;Let’s start with defining the basic terminology so we’re on the same page before jumping into some examples.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Python List Not Appending? Troubleshooting - Solutions for Beginners</title>
      <link>https://scripteverything.com/python-list-not-appending/</link>
      <pubDate>Tue, 10 Oct 2023 06:53:56 +0000</pubDate>
      <guid>https://scripteverything.com/python-list-not-appending/</guid>
      <description>&lt;p&gt;Why does a Python list not append an item?&lt;/p&gt;
&lt;p&gt;When you use the list method &lt;code&gt;.append()&lt;/code&gt; to insert a list item to the end of its existing contents, you could be in for a surprise if this doesn’t happen in some instances.&lt;/p&gt;
&lt;p&gt;The main reasons for why this may occur are due to the improper usage of the list method or adding a variable that doesn’t actually contain the information expected.&lt;/p&gt;</description>
    </item>
    <item>
      <title>2 Ways To Create A Python List With Identical Elements: One-Liners</title>
      <link>https://scripteverything.com/python-list-of-identical-elements/</link>
      <pubDate>Sun, 11 Jun 2023 16:09:44 +0000</pubDate>
      <guid>https://scripteverything.com/python-list-of-identical-elements/</guid>
      <description>&lt;p&gt;How can you create a list with identical elements in it with Python?&lt;/p&gt;
&lt;p&gt;There are two ways to create a list with identical elements in Python. One method is fairly straightforward and easy to remember, whereas the other involves the &lt;code&gt;itertools&lt;/code&gt; library.&lt;/p&gt;
&lt;p&gt;The one I apply the most in my Python coding is the method that uses the asterisk operator to a list containing the item I want to replicate. The other method uses the resources and techniques of a library with a function that is difficult to forget: &lt;code&gt;itertools.repeat()&lt;/code&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>Fix NameError: Python List Is Undefined</title>
      <link>https://scripteverything.com/python-list-is-undefined/</link>
      <pubDate>Sun, 11 Jun 2023 15:47:14 +0000</pubDate>
      <guid>https://scripteverything.com/python-list-is-undefined/</guid>
      <description>&lt;p&gt;How do you fix the &lt;code&gt;NameError: Python list is undefined&lt;/code&gt; type errors?&lt;/p&gt;
&lt;p&gt;When you get this error in your Python code there are a few ways to fix the problem and in essence, the easy fix is to check you’re working on a list variable that has been initiated &lt;em&gt;as a list&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The three common causes I have found in my own Python code for why this error pops up is due to: 1. not instantiating a variable as a list; 2. incorrectly typing the name of the list variable; or 3. using a variable in a list that hasn’t been declared.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Python List Not Sorting: Troubleshooting With Examples</title>
      <link>https://scripteverything.com/python-list-not-sorting/</link>
      <pubDate>Sat, 10 Jun 2023 22:02:01 +0000</pubDate>
      <guid>https://scripteverything.com/python-list-not-sorting/</guid>
      <description>&lt;p&gt;Why does a list not sort properly in Python as expected?&lt;/p&gt;
&lt;p&gt;There are a couple of reasons why a list may not have sorted as expected in Python. Here are a couple of ways to diagnose the problem to help you check.&lt;/p&gt;
&lt;p&gt;Here’s a quick list (no pun intended!) you can use to quickly check:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Are all elements within the list of the same data type?
&lt;ul&gt;
&lt;li&gt;Look at setting them to the same data type using the &lt;code&gt;key&lt;/code&gt; parameter&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Are you capturing the result of a &lt;code&gt;.sort()&lt;/code&gt; method?
&lt;ul&gt;
&lt;li&gt;Change this sort operation to the &lt;code&gt;sorted()&lt;/code&gt; function&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Are you sorting items with mixed case?
&lt;ul&gt;
&lt;li&gt;Apply the &lt;code&gt;key&lt;/code&gt; parameter converting all items to &lt;code&gt;.lower()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Do you understand the &lt;code&gt;key&lt;/code&gt; parameter?
&lt;ul&gt;
&lt;li&gt;Extract the &lt;code&gt;lambda&lt;/code&gt; function to check it works on elements in your list.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Are you sorting in reverse?
&lt;ul&gt;
&lt;li&gt;Check the &lt;code&gt;reverse&lt;/code&gt; parameter to your sort function. &lt;code&gt;False&lt;/code&gt; is the default setting it this parameter is not defined.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Are you sorting unicode characters?
&lt;ul&gt;
&lt;li&gt;Look to use a standard encoding type with the &lt;code&gt;key&lt;/code&gt; parameter using &lt;code&gt;lambda x : x.encode(&#39;utf-8&#39;)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Are there any whitespace characters wrapping the elements?
&lt;ul&gt;
&lt;li&gt;Apply the &lt;code&gt;.strip()&lt;/code&gt; string method to each element with the &lt;code&gt;key&lt;/code&gt; parameter.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let’s explore each of these items in a little more detail with examples. Here are some reasons why your Python lists may not be sorting properly:&lt;/p&gt;</description>
    </item>
    <item>
      <title>Python Lists: Order Alphabetically &amp; Sorting Examples</title>
      <link>https://scripteverything.com/python-list-order-alphabetically/</link>
      <pubDate>Sat, 10 Jun 2023 19:42:18 +0000</pubDate>
      <guid>https://scripteverything.com/python-list-order-alphabetically/</guid>
      <description>&lt;p&gt;How do you sort a Python list alphabetically?&lt;/p&gt;
&lt;p&gt;There are two easy ways to sort a list in Python: using the standard &lt;code&gt;sorted()&lt;/code&gt; function or using the &lt;code&gt;.sort()&lt;/code&gt; list method.&lt;/p&gt;
&lt;p&gt;Assuming you have a list of strings you can apply either method to sort the list alphabetically. If you have a list of numbers and strings you can use the parameter &lt;code&gt;key&lt;/code&gt; to change all values within the list to a &lt;code&gt;str&lt;/code&gt; data type and then perform the necessary sort (&lt;a href=&#34;#key-parameter&#34;&gt;more on that later&lt;/a&gt;).&lt;/p&gt;</description>
    </item>
    <item>
      <title>Copy List Using [:] In Python</title>
      <link>https://scripteverything.com/copy-list-using-in-python/</link>
      <pubDate>Sat, 25 Mar 2023 18:20:59 +0000</pubDate>
      <guid>https://scripteverything.com/copy-list-using-in-python/</guid>
      <description>&lt;p&gt;The empty slice operator &lt;code&gt;[:]&lt;/code&gt; in Python is a powerful and concise way of copying a list.&lt;/p&gt;
&lt;p&gt;It is shorthand syntax allowing you to create a new list that contains all the elements of the existing list where the operator is used. This operator is represented by one colon wrapped in square brackets &lt;code&gt;[:]&lt;/code&gt; with no values or spaces inside.&lt;/p&gt;
&lt;p&gt;While the empty slice operator may seem like a simple and straightforward way to copy a list, it is important to note that it only creates a shallow copy. This means that if the list contains mutable objects, such as other lists or dictionaries, the new list will still reference the objects contained in the original list. Therefore, any changes made to the original objects will also be reflected in the new copied list.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Why Does list1 = list2 Not Create A Copy In Python</title>
      <link>https://scripteverything.com/why-does-list1-equal-list2-not-create-a-copy/</link>
      <pubDate>Fri, 24 Mar 2023 22:11:13 +0000</pubDate>
      <guid>https://scripteverything.com/why-does-list1-equal-list2-not-create-a-copy/</guid>
      <description>&lt;p&gt;When starting out in Python it can be easy to think that the expression &lt;code&gt;list2 = list1&lt;/code&gt; will make &lt;code&gt;list2&lt;/code&gt; contain a copy of &lt;code&gt;list1&lt;/code&gt;. But it doesn’t take long to realise that this doesn’t meet expectations of what actually happens in the wild.&lt;/p&gt;
&lt;p&gt;Take the following code as an example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;list1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;list2&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;list1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;list1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;list1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;list2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Huh?&lt;/p&gt;
&lt;p&gt;Why did &lt;code&gt;list2&lt;/code&gt; contain the same contents as &lt;code&gt;list1&lt;/code&gt; even when the insertion of additional elements in &lt;code&gt;list1&lt;/code&gt; &lt;strong&gt;happened after &lt;code&gt;list2&lt;/code&gt; had already been assigned&lt;/strong&gt;?&lt;/p&gt;</description>
    </item>
    <item>
      <title>Sort List By Second (or Nth) Element In Python: 1 Line Of Code</title>
      <link>https://scripteverything.com/sort-list-second-element-python/</link>
      <pubDate>Sat, 05 Nov 2022 18:05:28 +0000</pubDate>
      <guid>https://scripteverything.com/sort-list-second-element-python/</guid>
      <description>&lt;p&gt;How do you sort a list of lists by the second element using Python?&lt;/p&gt;
&lt;p&gt;To sort a list of lists by the second element in each list use the &lt;code&gt;key&lt;/code&gt; parameter to either the &lt;code&gt;.sort()&lt;/code&gt; list function (if modifying the original list) or the &lt;code&gt;sorted()&lt;/code&gt; function (if returning a new list).&lt;/p&gt;
&lt;p&gt;Here’s an example demonstrating how you can do this with code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;list_2d&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;2nd&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;3rd&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;1st&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;list_2d&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sort&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;lambda&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;list_2d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;[[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;1st&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;2nd&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;3rd&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;As you can see from the above code the &lt;code&gt;key&lt;/code&gt; parameter in the &lt;code&gt;.sort()&lt;/code&gt; list function uses a &lt;code&gt;lambda&lt;/code&gt; expression which takes each list and extracts the second element. This is used as the key to determine how each list will be sorted.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Python: Sort List Of Tuples By First Element In 10 Seconds</title>
      <link>https://scripteverything.com/sort-list-of-tuples-by-first-element/</link>
      <pubDate>Thu, 16 Jun 2022 15:23:03 +0000</pubDate>
      <guid>https://scripteverything.com/sort-list-of-tuples-by-first-element/</guid>
      <description>&lt;p&gt;How do you sort a list of tuples by the first element in each tuple in Python?&lt;/p&gt;
&lt;p&gt;To sort a list of tuples in Python use the &lt;code&gt;.sort()&lt;/code&gt; list method if you want to modify the list to sort or the &lt;code&gt;sorted()&lt;/code&gt; function if you want to generate a new list.&lt;/p&gt;
&lt;p&gt;Use the parameter &lt;code&gt;key&lt;/code&gt; with either function and set the value of that parameter to a &lt;code&gt;lambda&lt;/code&gt; expression that returns the required tuple you want to sort by.&lt;/p&gt;</description>
    </item>
    <item>
      <title>How To Read File Into List With 1 Line Of Code In Python</title>
      <link>https://scripteverything.com/python-read-file-into-list/</link>
      <pubDate>Mon, 28 Mar 2022 21:37:57 +0000</pubDate>
      <guid>https://scripteverything.com/python-read-file-into-list/</guid>
      <description>&lt;p&gt;How do read the contents of a file in Python and insert these lines into a list?&lt;/p&gt;
&lt;p&gt;Using the built-in function &lt;code&gt;open()&lt;/code&gt; and the asterisk operator &lt;code&gt;*&lt;/code&gt; a file’s contents can easily be translated into a list with the following one-liner: &lt;code&gt;[*open(&#39;my_file.txt&#39;)]&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;what-does-open-function-do&#34;&gt;What Does &lt;code&gt;open()&lt;/code&gt; Function Do?&lt;/h2&gt;
&lt;p&gt;The built-in &lt;code&gt;open()&lt;/code&gt; function has one required parameter and several optional parameters. The required parameter is the location of the file. If the file is located in the current folder where the Python script is run you can insert the name of the file, for example, &lt;code&gt;test.txt&lt;/code&gt;.&lt;/p&gt;</description>
    </item>
    <item>
      <title>How To Prepend List With A New Value Or Contents Of Another List In Python</title>
      <link>https://scripteverything.com/prepend-list-python/</link>
      <pubDate>Mon, 28 Mar 2022 08:39:39 +0000</pubDate>
      <guid>https://scripteverything.com/prepend-list-python/</guid>
      <description>&lt;p&gt;How do you prepend a value or item to a list? Or how do you prepend the contents of a list to another list using Python?&lt;/p&gt;
&lt;p&gt;There are two ways to prepend items to an existing list in Python: for single items use the list method &lt;code&gt;.insert(idx, value)&lt;/code&gt; – this method will mutate the original list, or use the list concatenation operator &lt;code&gt;new_list + old_list&lt;/code&gt; which can be set to a new variable and prevent the original list being mutated.&lt;/p&gt;</description>
    </item>
    <item>
      <title>What Does [:-1] In Python Mean And Why Use It?</title>
      <link>https://scripteverything.com/square-bracket-minus-1-square-bracket-python/</link>
      <pubDate>Thu, 24 Mar 2022 10:14:59 +0000</pubDate>
      <guid>https://scripteverything.com/square-bracket-minus-1-square-bracket-python/</guid>
      <description>&lt;p&gt;What does &lt;code&gt;[:-1]&lt;/code&gt; in Python actually do and why would you want to use it?&lt;/p&gt;
&lt;p&gt;&lt;code&gt;[:-1]&lt;/code&gt; in Python is a slice operation used on strings or lists and captures all contents of the string or list except for the &lt;strong&gt;last character or element&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Here are some examples demonstrating the operation of this code with strings:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;my_string&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Why?&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;my_string&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[:&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;Why&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;As you can see from the simple code example above a variable &lt;code&gt;my_string&lt;/code&gt; contains a string that ends with a question mark. By using the &lt;code&gt;[:-1]&lt;/code&gt; slice operation it outputs the whole string except for the &lt;strong&gt;last character in the string&lt;/strong&gt;.&lt;/p&gt;</description>
    </item>
    <item>
      <title>What Does Asterisk Before Variable Mean In Python?</title>
      <link>https://scripteverything.com/what-does-asterisk-before-variable-name-mean-in-python/</link>
      <pubDate>Sat, 12 Mar 2022 22:21:04 +0000</pubDate>
      <guid>https://scripteverything.com/what-does-asterisk-before-variable-name-mean-in-python/</guid>
      <description>&lt;p&gt;What does the asterisk before a variable name mean in Python? For example, if I have a variable that contains a list of strings and the name of the variable is called &lt;code&gt;my_list&lt;/code&gt; what does &lt;code&gt;*my_list&lt;/code&gt; do?&lt;/p&gt;
&lt;p&gt;The asterisk is an operator in Python that is commonly known as the multiplication symbol when used between two numbers (&lt;code&gt;2 * 3&lt;/code&gt; will produce &lt;code&gt;6&lt;/code&gt;) but when it is inserted at the beginning of a variable, such as an iterable, like a list or dictionary, it expands the contents of that variable.&lt;/p&gt;</description>
    </item>
    <item>
      <title>How To Zip Two Or Multiple Lists Together In Python</title>
      <link>https://scripteverything.com/how-to-zip-two-or-multiple-lists-together-in-python/</link>
      <pubDate>Thu, 10 Mar 2022 22:41:57 +0000</pubDate>
      <guid>https://scripteverything.com/how-to-zip-two-or-multiple-lists-together-in-python/</guid>
      <description>&lt;p&gt;How do you zip two lists together in Python? What if you want to zip &lt;em&gt;multiple&lt;/em&gt; lists together, is it the same process or something different?&lt;/p&gt;
&lt;p&gt;The built-in function &lt;code&gt;zip()&lt;/code&gt; allows users to combine iterables, such as a list, into tuples by taking each corresponding item from the lists passed into its parameters merging the lists into one. It does not matter whether you have two or multiple lists the function are the list parameters are the same.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Python Code To Remove Duplicates From List - 1 Liner</title>
      <link>https://scripteverything.com/how-to-remove-duplicates-from-a-list-in-python-one-liner/</link>
      <pubDate>Tue, 11 Jan 2022 06:47:01 +0000</pubDate>
      <guid>https://scripteverything.com/how-to-remove-duplicates-from-a-list-in-python-one-liner/</guid>
      <description>&lt;p&gt;How do you remove duplicates from a list using Python?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To remove duplicate elements from a list in Python use a list comprehension to create a new list with no duplicates with this code: &lt;code&gt;[x for idx, x in enumerate(original_list) if x not in original_list[idx+1:]]&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here is an example demonstrating how this code works:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;original_list&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;idx&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;enumerate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;original_list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;not&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;original_list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;idx&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:]]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;As you can see from the example above the result from the list comprehension produces a unique list from the original.&lt;/p&gt;</description>
    </item>
    <item>
      <title>What Does [:] Mean In Python? Code Examples</title>
      <link>https://scripteverything.com/what-does-empty-slice-operator-mean-in-python/</link>
      <pubDate>Mon, 06 Dec 2021 14:22:09 +0000</pubDate>
      <guid>https://scripteverything.com/what-does-empty-slice-operator-mean-in-python/</guid>
      <description>&lt;p&gt;The slice operator enables you to capture a subset of data from an original list or string using the format &lt;code&gt;[start:stop:step]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Some popular use cases where I have used the slice operator include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Extracting year and/or month and/or day of the month from a string&lt;/li&gt;
&lt;li&gt;Extracting the zip code at the tail end of an address string&lt;/li&gt;
&lt;li&gt;Masking bank account or credit card numbers&lt;/li&gt;
&lt;li&gt;Extracting area code from phone numbers&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is how the &lt;strong&gt;Python [:]&lt;/strong&gt; operator can work when populating the values on either side of the colon:&lt;/p&gt;</description>
    </item>
    <item>
      <title>Inline For Loop With If Statements (Code Examples)</title>
      <link>https://scripteverything.com/python-inline-for-loop-with-if-statements/</link>
      <pubDate>Thu, 02 Dec 2021 05:56:28 +0000</pubDate>
      <guid>https://scripteverything.com/python-inline-for-loop-with-if-statements/</guid>
      <description>&lt;p&gt;What is the syntax for writing a for loop on one line in Python? This syntax is known as a &lt;em&gt;list comprehension&lt;/em&gt; and enables the user to write a for loop on one lin&lt;/p&gt;
&lt;p&gt;To write a for loop on one line in Python, known more commonly as the &lt;em&gt;list comprehension&lt;/em&gt;, wrap the for loop in a list like so: &lt;code&gt;[elem for elem in my_loop]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here is an example demonstrating how this code works:&lt;/p&gt;</description>
    </item>
    <item>
      <title>How To Create A List Of Zeros (Code Examples)</title>
      <link>https://scripteverything.com/create-list-of-zeros-in-python/</link>
      <pubDate>Wed, 01 Dec 2021 09:53:16 +0000</pubDate>
      <guid>https://scripteverything.com/create-list-of-zeros-in-python/</guid>
      <description>&lt;p&gt;Creating a list in Python can be super easy, but how do you populate that list with a bunch on zeroes?&lt;/p&gt;
&lt;p&gt;To create a list of zeroes in Python use the syntax &lt;code&gt;[0] * n&lt;/code&gt; where &lt;code&gt;n&lt;/code&gt; represents the number of items needed to create the list of zeroes required.&lt;/p&gt;
&lt;p&gt;Here’s an example demonstrating this use:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;my_zeros_list&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;my_zeros_list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;As you can see from the above example the list containing five elements are all populated with the value of 0.&lt;/p&gt;</description>
    </item>
    <item>
      <title>How To Check If Element In List Is Empty In Python</title>
      <link>https://scripteverything.com/check-if-element-in-list-is-empty-in-python/</link>
      <pubDate>Wed, 01 Dec 2021 07:10:04 +0000</pubDate>
      <guid>https://scripteverything.com/check-if-element-in-list-is-empty-in-python/</guid>
      <description>&lt;p&gt;How do you know if an element in Python is empty? First, you need to define what is meant by the term &lt;em&gt;empty&lt;/em&gt;. Does it mean &lt;code&gt;None&lt;/code&gt; or an &lt;a href=&#34;https://scripteverything.com/3-ways-to-check-if-a-string-is-empty-in-python-with-code-examples/&#34;&gt;empty string &lt;code&gt;&amp;quot;&amp;quot;&lt;/code&gt;&lt;/a&gt; or something else? For the examples in this article, I’ll assume that empty means an element is defined as &lt;code&gt;None&lt;/code&gt;, however, if you’ve defined empty as something else then you can simply substitute my &lt;code&gt;None&lt;/code&gt; references to your definition of an empty element.&lt;/p&gt;</description>
    </item>
    <item>
      <title>How To Add An Empty Element To A List In Python</title>
      <link>https://scripteverything.com/add-an-empty-element-to-a-list-in-python/</link>
      <pubDate>Wed, 01 Dec 2021 06:37:30 +0000</pubDate>
      <guid>https://scripteverything.com/add-an-empty-element-to-a-list-in-python/</guid>
      <description>&lt;p&gt;To insert or append an empty element into a Python list you need to first define what is meant by the word &lt;em&gt;empty&lt;/em&gt;. By &lt;em&gt;empty&lt;/em&gt; do you mean an empty string, &lt;code&gt;None&lt;/code&gt; or something else? While I may use &lt;code&gt;None&lt;/code&gt; in this post you can easily substitute it for something else according to your requirements.&lt;/p&gt;
&lt;p&gt;To add an empty element into a list either replace the existing element with the empty variable or if inserting it at the end use the list method &lt;code&gt;.append()&lt;/code&gt; or if inserting it at a specific place use the other list method &lt;code&gt;.insert()&lt;/code&gt;.&lt;/p&gt;</description>
    </item>
    <item>
      <title>How To Check If List Is Empty In Python</title>
      <link>https://scripteverything.com/how-to-check-if-list-is-empty-in-python/</link>
      <pubDate>Fri, 26 Nov 2021 19:03:17 +0000</pubDate>
      <guid>https://scripteverything.com/how-to-check-if-list-is-empty-in-python/</guid>
      <description>&lt;p&gt;Similar to the previous post on how to &lt;a href=&#34;https://scripteverything.com/3-ways-to-check-if-a-string-is-empty-in-python-with-code-examples/&#34;&gt;check if a string is empty&lt;/a&gt; the same principles and methods apply when checking if a list is empty in Python.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To check if a list is empty either use the direct approach by using an expression where a list is compared to an empty list or use the boolean expression with a &lt;code&gt;not&lt;/code&gt; operator (i.e. &lt;code&gt;if not my_empty_list:&lt;/code&gt;) or use the built-in function &lt;code&gt;len()&lt;/code&gt; to see if the length of the list is 0.&lt;/strong&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>Python: Sort List From Second (or Nth Index) On</title>
      <link>https://scripteverything.com/sort-list-after-nth-index-python/</link>
      <pubDate>Tue, 06 Jul 2021 12:38:55 +0000</pubDate>
      <guid>https://scripteverything.com/sort-list-after-nth-index-python/</guid>
      <description>&lt;p&gt;How do you sort a list by the second (or nth) element on in Python? How can you leave the first or nth elements in a list as they are but then have the remaining elements in the list sorted?&lt;/p&gt;
&lt;p&gt;When applying the &lt;code&gt;.sort()&lt;/code&gt; list method on a list Python will sort &lt;strong&gt;all&lt;/strong&gt; elements in that list, but what if you only wanted to sort after a certain index number?&lt;/p&gt;</description>
    </item>
    <item>
      <title>List Changes Unexpectedly In Python: How Can You Stop It?</title>
      <link>https://scripteverything.com/list-changes-unexpectedly-in-python-how-can-you-stop-it/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://scripteverything.com/list-changes-unexpectedly-in-python-how-can-you-stop-it/</guid>
      <description>&lt;p&gt;Why is it when you copy a list in Python doing &lt;code&gt;b_list = a_list&lt;/code&gt; that, any changes made to &lt;code&gt;a_list&lt;/code&gt; or to &lt;code&gt;b_list&lt;/code&gt; modify the other list?&lt;/p&gt;
&lt;p&gt;If you’ve played with lists in Python you will reach a point where you want to copy a list to make modifications to it without changing the original list. Initially you would think the following would work:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;a_list&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;b_list&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;a_list&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;b_list&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b_list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;a_list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;As you can see from the above example even though &lt;code&gt;b_list&lt;/code&gt; was the only list which had an extra element added to it, &lt;code&gt;a_list&lt;/code&gt; also changed!&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
