Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python List extend() Method

#1
Python List extend() Method

<div><p>How can you not one but multiple elements to a given list? Use the <code>extend()</code> method in Python. This tutorial shows you everything you need to know to help you master an essential method of the most fundamental container data type in the Python programming language.</p>
<h2>Definition and Usage</h2>
<p>The <code>list.extend(iter)</code> method adds all elements in the argument iterable <code>iter</code> to an existing <code>list</code>.</p>
<p>Here’s a short example:</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="">>>> lst = [1, 2, 3]
>>> lst.extend([4, 5, 6])
>>> lst
[1, 2, 3, 4, 5, 6]</pre>
<p>In the first line of the example, you create the list <code>lst</code>. You then append the integers <code>4, 5, 6</code> to the end of the list using the <code>extend()</code> method. The result is the list with six elements <code>[1, 2, 3, 4, 5, 6]</code>.</p>
<p>Try it yourself:</p>
<figure><iframe src="https://repl.it/repls/VoluminousVictoriousAssembly?lite=true" allowfullscreen="true" width="100%" height="400px"></iframe></figure>
<h2>Syntax</h2>
<p>You can call this method on each list object in Python. Here’s the syntax:</p>
<p><code>list.extend(iterable)</code></p>
<h2>Arguments</h2>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Argument</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>iterable</code></td>
<td>All the elements of the <a href="https://docs.python.org/3/glossary.html" target="_blank" rel="noreferrer noopener">iterable </a>will be added to the end of the list—in the order of their occurrence.</td>
</tr>
</tbody>
</table>
</figure>
<h2>Video</h2>
<figure class="wp-block-embed-youtube wp-block-embed is-type-rich is-provider-embed-handler wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="Python List extend() Method [No-BS]" width="1400" height="788" src="https://www.youtube.com/embed/o-zrozPt_CY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</p></div>
</figure>
<h2>Code Puzzle</h2>
<p>Now you know the basics. Let’s deepen your understanding with a short code puzzle—can you solve it?</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=""># Puzzle
# Author: Finxter Lee
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst1.append(lst2) lst3 = [1, 2, 3]
lst4 = [4, 5, 6]
lst3.extend(lst4) print(lst1 == lst3)
# What's the output of this code snippet?</pre>
<p>You can check out the solution on the <a href="https://app.finxter.com/learn/computer/science/470" target="_blank" rel="noreferrer noopener">Finxter app</a>. (I know it’s tricky!)</p>
<h2>Examples</h2>
<p>Let’s dive into a few more examples:</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="">>>> lst = [1, 2, 3]
>>> lst.extend({32, 42})
>>> lst
[1, 2, 3, 32, 42]
>>> lst.extend((1, 2))
>>> lst
[1, 2, 3, 32, 42, 1, 2]
>>> lst.extend(range(10,13))
>>> lst
[1, 2, 3, 32, 42, 1, 2, 10, 11, 12]
>>> lst.extend(1)
Traceback (most recent call last): File "&lt;pyshell#10>", line 1, in &lt;module> lst.extend(1)
TypeError: 'int' object is not iterable
>>> </pre>
<p>You can see that the <code>extend()</code> method allows for all sorts of iterables: lists, sets, tuples, and even range objects. But what it doesn’t allow is an integer argument. Why? Because the integer argument isn’t an iterable—it doesn’t make sense to “iterate over all values in an integer”.</p>
<h2>Python List extend() At The Beginning</h2>
<p>What if you want to use the extend() method at the beginning: you want to “add” a number of elements just before the first element of the list. </p>
<p>Well, you should work on your terminology for starters. But if you insist, you can use the <code>insert()</code> method instead. </p>
<p>Here’s an example:</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="">>>> lst = [1, 2, 3]
>>> lst.insert(0, 99)
>>> lst
[99, 1, 2, 3]</pre>
<p>The <code>insert(i, x)</code> method inserts an element <code>x</code> at position <code>i</code> in the list. This way, you can insert an element to each position in the list—even at the first position. Note that if you insert an element at the first position, each subsequent element will be moved by one position. In other words, element <code>i</code> will move to position <code>i+1</code>. </p>
<h2>Python List extend() vs +</h2>
<p><strong>List concatenation operator +:</strong> If you use the + operator on two integers, you’ll get the sum of those integers. But if you use the + operator on two lists, you’ll get a new list that is the concatenation of those lists.</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="">l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = l1 + l2
print(l3)</pre>
<p>Output:</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="">[1, 2, 3, 4, 5, 6]</pre>
<p>The problem with the + operator for list concatenation is that it creates a new list for each list concatenation operation. This can be very inefficient if you use the + operator multiple times in a loop.</p>
<p>How fast is the + operator really? Here’s a common scenario how people use it to add new elements to a list in a loop. This is very inefficient:</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="">import time start = time.time() l = []
for i in range(100000): l = l + [i] stop = time.time() print("Elapsed time: " + str(stop - start))</pre>
<p>Output:</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="">Elapsed time: 14.438847541809082</pre>
<p>The experiments were performed on my notebook with an Intel® Core™ i7-8565U 1.8GHz processor (with Turbo Boost up to 4.6 GHz) and 8 GB of RAM.</p>
<p>I measured the start and stop timestamps to calculate the total elapsed time for adding 100,000 elements to a list.</p>
<p>The result shows that it takes 14 seconds to perform this operation.</p>
<p>This seems slow (it is!). So let’s investigate some other methods to concatenate and their performance:</p>
<h2>Python List extend() Performance</h2>
<p>Here’s a similar example that shows how you can use the <code>extend()</code> method to concatenate two lists <code>l1</code> and <code>l2</code>.</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="">l1 = [1, 2, 3]
l2 = [4, 5, 6]
l1.extend(l2)
print(l1)</pre>
<p>Output:</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="">[1, 2, 3, 4, 5, 6]</pre>
<p>But is it also fast? Let’s check the performance!</p>
<p><strong>Performance:</strong></p>
<p>I performed a similar experiment as before for the list concatenation operator <code>+</code>.</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="">import time start = time.time() l = []
l.extend(range(100000)) stop = time.time() print("Elapsed time: " + str(stop - start))
</pre>
<p>Output:</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="">Elapsed time: 0.0</pre>
<p>I measured the start and stop timestamps to calculate the total elapsed time for adding 100,000 elements to a list.</p>
<p>The result shows that it takes negligible time to run the code (0.0 seconds compared to 0.006 seconds for the <code>append()</code> operation above).</p>
<p><strong>The <code>extend()</code> method is the most concise and fastest way to concatenate lists.</strong></p>
<h2>Python List append() vs extend()</h2>
<p>I shot a small video explaining the difference and which method is faster, too:</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 append() vs extend() - Semantic and Speed Difference" width="1400" height="788" src="https://www.youtube.com/embed/VGg8sNJ9kOM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>The method <code>list.append(x)</code> adds element <code>x</code> to the end of the <code>list</code>. </p>
<p>The method <code>list.extend(iter)</code> adds all elements in <code>iter</code> to the end of the <code>list</code>.</p>
<p><strong>The difference between <code>append()</code> and <code>extend()</code> is that the former adds only one element and the latter adds a collection of elements to the list.</strong></p>
<figure class="wp-block-image size-large"><img src="https://blog.finxter.com/wp-content/uploads/2020/03/append-1024x576.jpg" alt="" class="wp-image-6712" srcset="https://blog.finxter.com/wp-content/uploads/2020/03/append-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>You can see this in the following example:</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(1)
>>> l.append(2)
>>> l
[1, 2]
>>> l.extend([3, 4, 5])
>>> l
[1, 2, 3, 4, 5]</pre>
<p>In the code, you first add integer elements 1 and 2 to the list using two calls to the <code>append()</code> method. Then, you use the extend method to add the three elements 3, 4, and 5 in a single call of the <code>extend()</code> method.</p>
<h3><strong>Which method is faster — extend() vs append()?</strong></h3>
<p>To answer this question, I’ve written a short script that tests the runtime performance of creating large lists of increasing sizes using the <code>extend()</code> and the <code>append()</code> methods. </p>
<p>Our thesis is that the <code>extend()</code> method should be faster for larger list sizes because Python can append elements to a list in a batch rather than by calling the same method again and again.</p>
<p>I used my notebook with an Intel® Core™ i7-8565U 1.8GHz processor (with Turbo Boost up to 4.6 GHz) and 8 GB of RAM. </p>
<p>Then, I created 100 lists with both methods, <code>extend()</code> and <code>append()</code>, with sizes ranging from 10,000 elements to 1,000,000 elements. As elements, I simply incremented integer numbers by one starting from 0.</p>
<p>Here’s the code I used to measure and plot the results: which method is faster—<code>append()</code> or <code>extend()</code>?</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="">import time def list_by_append(n): '''Creates a list &amp; appends n elements''' lst = [] for i in range(n): lst.append(n) return lst def list_by_extend(n): '''Creates a list &amp; extends it with n elements''' lst = [] lst.extend(range(n)) return lst # Compare runtime of both methods
list_sizes = [i * 10000 for i in range(100)]
append_runtimes = []
extend_runtimes = [] for size in list_sizes: # Get time stamps time_0 = time.time() list_by_append(size) time_1 = time.time() list_by_extend(size) time_2 = time.time() # Calculate runtimes append_runtimes.append((size, time_1 - time_0)) extend_runtimes.append((size, time_2 - time_1)) # Plot everything
import matplotlib.pyplot as plt
import numpy as np append_runtimes = np.array(append_runtimes)
extend_runtimes = np.array(extend_runtimes) print(append_runtimes)
print(extend_runtimes) plt.plot(append_runtimes[:,0], append_runtimes[:,1], label='append()')
plt.plot(extend_runtimes[:,0], extend_runtimes[:,1], label='extend()') plt.xlabel('list size')
plt.ylabel('runtime (seconds)') plt.legend()
plt.savefig('append_vs_extend.jpg')
plt.show()</pre>
<p>The code consists of three high-level parts:</p>
<ul>
<li>In the first part of the code, you define two functions <code>list_by_append(n)</code> and <code>list_by_extend(n)</code> that take as input argument an integer list size <code>n</code> and create lists of successively increasing integer elements using the <code>append()</code> and <code>extend()</code> methods, respectively.</li>
<li>In the second part of the code, you compare the runtime of both functions using 100 different values for the list size <code>n</code>. </li>
<li>In the third part of the code, you plot everything using the Python <a rel="noreferrer noopener" href="https://blog.finxter.com/matplotlib-line-plot/" target="_blank">matplotlib library</a>.</li>
</ul>
<p>Here’s the resulting plot that compares the runtime of the two methods append() vs extend(). On the x axis, you can see the list size from 0 to 1,000,000 elements. On the y axis, you can see the runtime in seconds needed to execute the respective functions.</p>
<figure class="wp-block-image size-large"><img src="https://blog.finxter.com/wp-content/uploads/2020/03/append_vs_extend-2.jpg" alt="" class="wp-image-6633" srcset="https://blog.finxter.com/wp-content/uploads/2020/03/append_vs_extend-2.jpg 640w, https://blog.finxter.com/wp-content/uplo...00x225.jpg 300w" sizes="(max-width: 640px) 100vw, 640px" /></figure>
<p>The resulting plot shows that both methods are extremely fast for a few tens of thousands of elements. In fact, they are so fast that the <code>time()</code> function of the <a rel="noreferrer noopener" href="https://docs.python.org/2/library/time.html#time.time" target="_blank">time module</a> cannot capture the elapsed time.</p>
<p>But as you increase the size of the lists to hundreds of thousands of elements, the <code>extend()</code> method starts to win:</p>
<p><strong>For large lists with one million elements, the runtime of the <code>extend()</code> method is 60% faster than the runtime of the <code>append()</code> method.</strong></p>
<p>The reason is the already mentioned batching of individual append operations. </p>
<p>However, the effect only plays out for very large lists. For small lists, you can choose either method. Well, for clarity of your code, it would still make sense to prefer <code>extend()</code> over <code>append()</code> if you need to add a bunch of elements rather than only a single element. </p>
<h2>Python Append List to Another List</h2>
<p>To append list <code>lst_1</code> to another list <code>lst_2</code>, use the <code>lst_2.extend(lst_1)</code> method. Here’s an example:</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="">>>> lst_1 = [1, 2, 3]
>>> lst_2 = [4, 5, 6]
>>> lst_2.extend(lst_1)
>>> lst_2
[4, 5, 6, 1, 2, 3]</pre>
<h2>Python List extend() Returns None</h2>
<p>The return value of the <code>extend()</code> method is <code>None</code>. The return value of the <code>extend()</code> method is not a list with the added elements. Assuming this is a common source of mistakes.</p>
<p>Here’s such an error where the coder wrongly assumed this:</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="">>>> lst = [1, 2].extend([3, 4])
>>> lst[0]
Traceback (most recent call last): File "&lt;pyshell#16>", line 1, in &lt;module> lst[0]
TypeError: 'NoneType' object is not subscriptable</pre>
<p> It doesn’t make sense to assign the result of the <code>extend()</code> method to another variable—because it’s always <code>None</code>. Instead, the <code>extend()</code> method changes a list object without creating (and returning) a new list.</p>
<p>Here’s the correct version of the same code:</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="">>>> lst = [1, 2]
>>> lst.extend([3, 4])
>>> lst[0]
1</pre>
<p>Now, you change the list object itself by calling the <code>extend()</code> method on it. You through away the <code>None</code> return value because it’s not needed.</p>
<h2>Python List Concatenation</h2>
<p>So you have two or more lists and you want to glue them together. This is called <strong>list concatenation</strong>. How can you do that?</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="How to Concatenate Lists in Python? [Interactive Guide]" width="1400" height="788" src="https://www.youtube.com/embed/9rrqInUeG8U?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>These are six ways of concatenating lists (<a href="https://blog.finxter.com/concatenate-lists-in-python/" target="_blank" rel="noreferrer noopener">detailed tutorial here</a>):</p>
<ol>
<li>List concatenation operator <code>+</code></li>
<li>List <code>append()</code> method</li>
<li>List <code>extend()</code> method</li>
<li>Asterisk operator <code>*</code></li>
<li><code>Itertools.chain()</code></li>
<li>List comprehension</li>
</ol>
<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="">a = [1, 2, 3]
b = [4, 5, 6] # 1. List concatenation operator +
l_1 = a + b # 2. List append() method
l_2 = [] for el in a: l_2.append(el) for el in b: l_2.append(el) # 3. List extend() method
l_3 = []
l_3.extend(a)
l_3.extend(b) # 4. Asterisk operator *
l_4 = [*a, *b] # 5. Itertools.chain()
import itertools
l_5 = list(itertools.chain(a, b)) # 6. List comprehension
l_6 = [el for lst in (a, b) for el in lst]</pre>
<p>Output:</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_1 --> [1, 2, 3, 4, 5, 6]
l_2 --> [1, 2, 3, 4, 5, 6]
l_3 --> [1, 2, 3, 4, 5, 6]
l_4 --> [1, 2, 3, 4, 5, 6]
l_5 --> [1, 2, 3, 4, 5, 6]
l_6 --> [1, 2, 3, 4, 5, 6] '''</pre>
<p><strong>What’s the best way to concatenate two lists?</strong></p>
<p>If you’re busy, you may want to know the best answer immediately. Here it is:</p>
<p><strong>To concatenate two lists <code>l1</code>, <code>l2</code>, use the <code>l1.extend(l2)</code> method which is the fastest and the most readable. </strong></p>
<p><strong>To concatenate more than two lists, use the unpacking (asterisk) operator <code>[*l1, *l2, ..., *ln]</code>.</strong></p>
<p>However, you should avoid using the append() method for list concatenation because it’s neither very efficient nor concise and readable.</p>
<h2>Python List extend() Unique – Add If Not Exists</h2>
<p>A common question is the following:</p>
<p><strong>How can you add or append elements to a list, but only if they don’t already exist in the list?</strong></p>
<p>When ignoring any performance issues, the answer is simple: use an if condition in combination with the membership operation <code>element in list</code> and only <a href="https://blog.finxter.com/python-list-append/" target="_blank" rel="noreferrer noopener"><code>append()</code> the element</a> if the result is <code>False</code> (don’t use <code>extend()</code> for this fine-grained method). As an alternative, you can also use the negative membership operation <code>element not in list</code> and add the element if the result is <code>True</code>.</p>
<p><strong>Example</strong>: Say, you want to add all elements between 0 and 9 to a list of three elements. But you don’t want any duplicates. Here’s how you can do this:</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="">lst = [1, 2, 3]
for element in range(10): if element not in lst: lst.append(element) </pre>
<p>Resulting list:</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="">[1, 2, 3, 0, 4, 5, 6, 7, 8, 9]</pre>
<p>You add all elements between 0 and 9 to the list but only if they aren’t already present. Thus, the resulting list doesn’t contain duplicates.</p>
<p>But there’s a problem: this method is highly inefficient!</p>
<p>In each loop iteration, the snippet <code>element not in lst</code> searches the whole list for the current <code>element</code>. For a list with <code>n</code> elements, this results in <code>n</code> comparisons, per iteration. As you have <code>n</code> iterations, the runtime complexity of this code snippet is quadratic in the number of elements.</p>
<p>Can you do better?</p>
<p>Sure, but you need to look beyond the list data type: Python sets are the right abstraction here. <a rel="noreferrer noopener" href="https://blog.finxter.com/sets-in-python/" target="_blank">If you need to refresh your basic understanding of the set data type, check out my detailed set tutorial (with Harry Potter examples) on the Finxter blog.</a></p>
<p>Why are Python sets great for this? Because they don’t allow any duplicates per design: <em>a set is a <strong>unique </strong>collection of unordered elements</em>. And the <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Time_complexity" target="_blank">runtime complexity</a> of the membership operation is not linear in the number of elements (as it’s the case for lists) but constant!</p>
<p><strong>Example</strong>: Say, you want to add all elements between 0 and 9 to a set of three elements. But you don’t want any duplicates. Here’s how you can do this with sets:</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="">s = {1, 2, 3}
for element in range(10): s.add(element) print(s)
</pre>
<p>Resulting set:</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="">{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}</pre>
<p>The set doesn’t allow for duplicate entries so the elements 1, 2, and 3 are not added twice to the set. </p>
<p>You can even make this code more concise:</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="">s = {1, 2, 3}
s = s.union(range(10)) print(s)</pre>
<p>Output:</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="">{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}</pre>
<p>The union method creates a new set that consists of all elements in both operands.</p>
<p>Now, you may want to have a list as a result and not a set. The solution is simple: convert the resulting set to a list by using the <code>list(set)</code> conversion method. This has linear runtime complexity and if you call it only once, it doesn’t change the overall runtime complexity of the code snippet (it remains linear in the number of set elements). </p>
<p><strong>Problem: what if you want to maintain the order information and still add all elements that are not already in the list?</strong></p>
<p>The problem with the previous approach is that by converting the list to a set, the order of the list is lost. In this case, I’d advise you to do the following: use two data structures, a list and a set. You use the list to add new elements and keep the order information. You use the set to check membership (<a rel="noreferrer noopener" href="https://blog.finxter.com/sets-in-python/" target="_blank">constant rather than linear runtime complexity</a>). Here’s the code:</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="">lst = [1, 2, 3]
s = set(lst) for element in range(10): if element not in s: s.add(element) lst.append(element) print(lst)</pre>
<p>Resulting list:</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="">[1, 2, 3, 0, 4, 5, 6, 7, 8, 9]</pre>
<p>You can see that the resulting list doesn’t contain any duplicates but the order information is maintained. At the same time, the runtime complexity of the code is linear because each loop iteration can be completed in constant time. </p>
<p>The trade-off is that you have to maintain two data structures which results in double the memory overhead. This nicely demonstrates the common inverse relationship between memory and runtime overhead.</p>
<h2>Python List extend() Return New List</h2>
<p>If you use the <code>lst.extend(iter)</code> operation, you add the elements in <code>iter</code> to the existing list <code>lst</code>. But what if you want to create a new list where all elements were added?</p>
<p>The answer is simply to use the <a rel="noreferrer noopener" href="https://blog.finxter.com/concatenate-lists-in-python/" target="_blank">list concatenation</a> operation <code>lst + list(iter)</code> which creates a new list each time it is used. The original list <code>lst</code> will not be affected by the list concatenation operation.</p>
<p>Here’s an example that shows that the <code>extend()</code> method only modifies an existing list:</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="">>>> lst_1 = [1, 2, 3]
>>> lst_2 = lst_1.extend([42, 99])
>>> lst_1
[1, 2, 3, 42, 99]</pre>
<p>And here’s the example that shows how to create a new list as you add elements 42 and 99 to a list:</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="">>>> lst_3 = [1, 2, 3]
>>> lst_4 = lst_3 + [42, 99]
>>> lst_3
[1, 2, 3]</pre>
<p>By using the list concatenation operation, you can create a new list rather than appending the element to an existing list.</p>
<h2>Python List extend() Time Complexity, Memory, and Efficiency</h2>
<p><strong>Time Complexity: </strong>The <code>extend()</code> method has linear <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Time_complexity" target="_blank">time complexity</a> <em>O(n)</em> in the number of elements <em>n</em> to be added to the list. Adding one element to the list requires only a constant number of operations—no matter the size of the list.</p>
<p><strong>Space Complexity: </strong>The <code>extend()</code> method has linear space complexity <em>O(n)</em> in the number of elements <em>n</em> to be added to the list. The operation itself needs only a constant number of bytes for the involved temporary variables. The memory overhead does not depend on the size of the list.</p>
<p><a rel="noreferrer noopener" href="https://blog.finxter.com/concatenate-lists-in-python/" target="_blank">If you’re interested in the most performant ways to add multiple elements to a list, you can see extensive performance tests in this tutorial on the Finxter blog.</a></p>
<h2>Python List extend() at Index</h2>
<p><strong>If you want to insert a whole list at a certain position and create a new list by doing so, I’d recommend to use Python slicing.</strong> <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank">Check out this in-depth blog tutorial that’ll show you everything you need to know about slicing.</a></p>
<p>Here’s the code that shows how to create a new list after inserting a list at a certain position:</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="">>>> lst = [33, 44, 55]
>>> lst[:2] + [99, 42] + lst[2:]
[33, 44, 99, 42, 55]</pre>
<p>Again, you’re using<a href="https://blog.finxter.com/concatenate-lists-in-python/" target="_blank" rel="noreferrer noopener"> list concatenation</a> to create a new list with element 99 inserted at position 2. Note that the slicing operations <code>lst[:2]</code> and <code>lst[2:]</code> create their own shallow copy of the list.</p>
<h2>Python List extend() Thread Safe</h2>
<p>Do you have a multiple threads that access your list at the same time? Then you need to be sure that the list operations (such as <code>extend()</code>) are actually <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Thread_safety" target="_blank">thread safe</a>. </p>
<p>In other words: can you call the <code>extend()</code> operation in two threads on the same list at the same time? (And can you be sure that the result is meaningful?)</p>
<p>The answer is yes (if you use the <a href="https://github.com/python/cpython" target="_blank" rel="noreferrer noopener">cPython </a>implementation). The reason is Python’s <a rel="noreferrer noopener" href="https://wiki.python.org/moin/GlobalInterpreterLock" target="_blank">global interpreter lock</a> that ensures that a thread that’s currently working on it’s code will first finish its current basic Python operation as defined by the cPython implementation. Only if it terminates with this operation will the next thread be able to access the computational resource. This is ensured with a sophisticated locking scheme by the cPython implementation. </p>
<p>The only thing you need to know is that each basic operation in the cPython implementation is <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Atomicity_(database_systems)" target="_blank">atomic</a>. It’s executed wholly and at once before any other thread has the chance to run on the same virtual engine. Therefore, there are no race conditions. An example for such a race condition would be the following: the first thread reads a value from the list, the second threads overwrites the value, and the first thread overwrites the value again invalidating the second thread’s operation.</p>
<p><strong>All cPython operations are thread-safe. </strong>But if you combine those operations into higher-level functions, those are not generally thread safe as they consist of many (possibly interleaving) operations.</p>
<h2>Where to Go From Here?</h2>
<p>The <code>list.extend(iter)</code> method adds all elements in <code>iter</code> to the end of the <code>list</code> (in the order of their appearance).</p>
<p>You’ve learned the ins and outs of this important <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-methods/" target="_blank">Python list method</a>.</p>
<p>If you keep struggling with those basic Python commands and you feel stuck in your learning progress, I’ve got something for you: <a rel="noreferrer noopener" href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank">Python One-Liners</a> (Amazon Link). </p>
<p>In the book, I’ll give you a thorough overview of critical computer science topics such as machine learning, regular expression, data science, NumPy, and Python basics—all in a single line of Python code!</p>
<p><a rel="noreferrer noopener" href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank">Get the book from Amazon!</a></p>
<p><strong>OFFICIAL BOOK DESCRIPTION:</strong> <em>Python One-Liners will show readers how to perform useful tasks with one line of Python code. Following a brief Python refresher, the book covers essential advanced topics like slicing, list comprehension, broadcasting, lambda functions, algorithms, regular expressions, neural networks, logistic regression and more. Each of the 50 book sections introduces a problem to solve, walks the reader through the skills necessary to solve that problem, then provides a concise one-liner Python solution with a detailed explanation.</em></p>
</div>


https://www.sickgaming.net/blog/2020/03/...nd-method/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016