Sick Gaming
[Tut] Python List Methods - 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] Python List Methods (/thread-93905.html)



[Tut] Python List Methods - xSicKxBot - 03-07-2020

Python List Methods

<div><p>The most important collection data type in Python is the <code>list</code> data type. You’ll use lists basically in all your future projects so take 3-5 minutes and study this short guide carefully.</p>
<p>You can also play my short video tutorial as you read over the methods:</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 List Methods" width="1400" height="788" src="https://www.youtube.com/embed/4j8B9a_kZY4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Method</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>lst.append(x)</code></td>
<td>Appends element <code>x</code> to the list <code>lst</code>.</td>
</tr>
<tr>
<td><code><code>lst</code>.clear()</code></td>
<td>Removes all elements from the list <code>lst</code>–which becomes empty.</td>
</tr>
<tr>
<td><code>lst.copy()</code></td>
<td>Returns a copy of the list <code>lst</code>. Copies only the list, not the elements in the list (shallow copy).</td>
</tr>
<tr>
<td><code>lst.count(x)</code></td>
<td>Counts the number of occurrences of element <code>x</code> in the list <code>lst</code>.</td>
</tr>
<tr>
<td><code>lst.extend(iter)</code></td>
<td>Adds all elements of an iterable <code>iter</code>(e.g. another list) to the list <code>lst</code>. </td>
</tr>
<tr>
<td><code>lst.index(x)</code></td>
<td>Returns the position (index) of the first occurrence of value <code>x</code> in the list <code>lst</code>. </td>
</tr>
<tr>
<td><code>lst.insert(i, x)</code></td>
<td>Inserts element <code>x</code> at position (index) <code>i</code> in the list <code>lst</code>.</td>
</tr>
<tr>
<td><code>lst.pop()</code></td>
<td>Removes and returns the final element of the list <code>lst</code>. </td>
</tr>
<tr>
<td><code>lst.remove(x)</code></td>
<td>Removes and returns the first occurrence of element <code>x</code> in the list <code>lst</code>. </td>
</tr>
<tr>
<td><code>lst.reverse()</code></td>
<td>Reverses the order of elements in the list <code>lst</code>. </td>
</tr>
<tr>
<td><code>lst.sort()</code></td>
<td>Sorts the elements in the list <code>lst</code> in ascending order.</td>
</tr>
</tbody>
</table>
</figure>
<p>If you’ve studied the table carefully, you’ll know the most important list methods in Python. Let’s have a look at some examples of above methods:</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 = []
>>> l.append(2)
>>> l
[2]
>>> l.clear()
>>> l
[]
>>> l.append(2)
>>> l
[2]
>>> l.copy()
[2]
>>> l.count(2)
1
>>> l.extend([2,3,4])
>>> l
[2, 2, 3, 4]
>>> l.index(3)
2
>>> l.insert(2, 99)
>>> l
[2, 2, 99, 3, 4]
>>> l.pop()
4
>>> l.remove(2)
>>> l
[2, 99, 3]
>>> l.reverse()
>>> l
[3, 99, 2]
>>> l.sort()
>>> l
[2, 3, 99]</pre>
<h2>Action Steps:</h2>
<ul>
<li>Master <a rel="noreferrer noopener" aria-label="Python Sets (opens in a new tab)" href="https://blog.finxter.com/sets-in-python/" target="_blank">Python Sets</a> (with Harry Potter Examples)</li>
<li>Master <a rel="noreferrer noopener" aria-label="Python Dictionaries (opens in a new tab)" href="https://blog.finxter.com/python-dictionary/" target="_blank">Python Dictionaries</a> (the ultimate blog tutorial)</li>
<li>Join my <a rel="noreferrer noopener" aria-label="free email computer science academy (opens in a new tab)" href="https://blog.finxter.com/subscribe/" target="_blank">free email computer science academy</a> for continuous improvement in Python</li>
</ul>
<p>Thanks for taking the time to reading this article! <img src="https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f40d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
</div>


https://www.sickgaming.net/blog/2020/03/06/python-list-methods/