Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python List to Set Conversion [Interactive Guide]

#1
Python List to Set Conversion [Interactive Guide]

<div><p><strong>Do you have a list but you want to convert it to a Python set? No problem! Use the <code>set(...)</code> constructor and pass the list object as an argument. For example, if you have a list of strings <code>friends</code>, you can convert it to a set using the call <code>set(friends)</code>. </strong></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 to Set Conversion [Interactive Guide]" width="1400" height="788" src="https://www.youtube.com/embed/qywPWgnCqKw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>Here’s an example code snippet:</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="">friends = ['Alice', 'Bob', 'Ann', 'Liz']
s = set(friends)
print(s)
# {'Ann', 'Alice', 'Bob', 'Liz'}
</pre>
<p>Try it yourself in our interactive Python shell:</p>
<p> <iframe src="https://trinket.io/embed/python/e1b893ab3c" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Add another string <code>'Alice'</code> to the list <code>friends</code> and see the resulting set. How many elements do the list and the set have?</em></p>
<h2>Python List to Set Time Complexity</h2>
<p>The <a href="https://blog.finxter.com/runtime-complexity-of-python-list-methods-easy-table-lookup/" target="_blank" rel="noreferrer noopener">time complexity</a> of converting a <a href="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener">list </a>to a <a href="https://blog.finxter.com/sets-in-python/" target="_blank" rel="noreferrer noopener">set </a>is linear in the number of list elements. So, if the set has <em>n</em> elements, the asymptotic complexity is <em>O(n)</em>. The reason is that you need to<a href="https://blog.finxter.com/python-loops/" target="_blank" rel="noreferrer noopener"> iterate over each element</a> in the list which is <em>O(n)</em>, and add this element to the set which is <em>O(1)</em>. Together the complexity is <em>O(n) * O(1) = O(n * 1) = O(n)</em>. </p>
<p>Here’s the pseudo-code implementation of the<em> list to set</em> conversion method:</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="">def list_to_set(l): s = set() # Repeat n times --> O(n) for x in l: # Add element to set --> O(1) s.add(x) return s friends = ['Alice', 'Bob', 'Ann', 'Liz', 'Alice']
s = list_to_set(friends)
print(s)
# {'Ann', 'Alice', 'Bob', 'Liz'}</pre>
<p>Need help understanding this code snippet? Try visualizing it in your browser—just click “Next” to see what the code does in memory:</p>
<p> <iframe width="800" height="500" frameborder="0" src="https://pythontutor.com/iframe-embed.html#code=def%20list_to_set%28l%29%3A%0A%20%20%20%20s%20%3D%20set%28%29%0A%0A%20%20%20%20%23%20Repeat%20n%20times%20--%3E%20O%28n%29%0A%20%20%20%20for%20x%20in%20l%3A%0A%0A%20%20%20%20%20%20%20%20%23%20Add%20element%20to%20set%20--%3E%20O%281%29%0A%20%20%20%20%20%20%20%20s.add%28x%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20return%20s%0A%0Afriends%20%3D%20%5B'Alice',%20'Bob',%20'Ann',%20'Liz',%20'Alice'%5D%0As%20%3D%20list_to_set%28friends%29%0Aprint%28s%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe> </p>
<h2>Python List to Set Remove Duplicates</h2>
<p>The <strong>set data structure is one of the basic collection data types</strong> in Python and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-vs-go-which-language-you-should-choose/" target="_blank">many other programming languages</a>. </p>
<p><strong>A set is an unordered collection of unique elements.</strong> (Read more on the <a href="https://blog.finxter.com/sets-in-python/" target="_blank" rel="noreferrer noopener">Ultimate Guide to Python Sets</a>)</p>
<ul>
<li><strong>Collection</strong>: A set is a collection of elements like a list or a <a href="https://blog.finxter.com/convert-tuple-to-list/" target="_blank" rel="noreferrer noopener">tuple</a>. The collection consists of either primitive elements (e.g. integers, floats, strings), or complex elements (e.g. <a href="https://blog.finxter.com/a-simple-example-for-python-objects-and-classes-video/" target="_blank" rel="noreferrer noopener">objects</a>, tuples). However, in a set all data elements must be hashable because it heavily relies on the hash function to implement the specification.</li>
<li><strong>Unordered</strong>: Unlike lists, sets are unordered because there is no fixed order of the elements. In other words, regardless of the order you put stuff in the set, you can never be sure in which order the set stores these elements.</li>
<li><strong>Unique</strong>: All elements in the set are unique. Each pair of values <code>(x,y)</code> in the set produces a different pair of hash values <code>(hash(x)!=hash(y))</code>. Hence, each pair of elements <code>x</code> and <code>y</code> in the set are different.</li>
</ul>
<p>Thus, you can <a href="https://blog.finxter.com/how-to-remove-duplicates-from-a-python-list/" target="_blank" rel="noreferrer noopener">remove all duplicates</a> from a list <code>x</code> by converting it into a set and back into a list using the command <code>list(set(x))</code>. However, the ordering information may be lost in the process (as a set is, by definition, unordered). </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="">friends = ['Alice', 'Bob', 'Ann', 'Liz', 'Alice', 'Bob']
print(friends)
# ['Alice', 'Bob', 'Ann', 'Liz', 'Alice', 'Bob'] dup_free = list(set(friends))
print(dup_free)
# ['Bob', 'Alice', 'Liz', 'Ann']</pre>
<p>This way, the resulting list doesn’t have any duplicates—but it also has lost the order of elements: strings <code>'Liz'</code> and <code>'Ann'</code> switched their order after conversion. This may be different on your computer!</p>
<p> <iframe src="https://trinket.io/embed/python/bac3b8ba97" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<h2>Python List to Set to List: list(set(x))</h2>
<p>By converting a list <code>x</code> to a set and back to a list with the <a href="https://blog.finxter.com/how-does-nested-list-comprehension-work-in-python/" target="_blank" rel="noreferrer noopener">nested</a> constructor expression <code>list(set(x))</code>, you achieve two things:</p>
<ul>
<li>You remove all duplicates from the original list <code>x</code>.</li>
<li>You lose all ordering information. The resulting list may (or may not) have a complete new ordering of the remaining elements.</li>
</ul>
<p>There’s no way out: the set data structure is more efficient than the list data structure only because it’s less powerful. </p>
<p><em>It’s like compressing an image: by removing information from the original image, the new image needs less resources on your computer at the cost of having a lower quality. If you convert the lossy-compressed image (or the set for that matter) back into the original data structure, it doesn’t look the same anymore. </em></p>
<p>This highlights an important trade-off in programming: <strong>always choose the right data structure for the particular problem at hand.</strong></p>
<h2>Python List to Set Preserve Order</h2>
<p>But what if you want to preserve the order when converting a list to a set (and, maybe, back)? (You’d only do this to remove duplicates).</p>
<p><a href="https://blog.finxter.com/how-to-remove-duplicates-from-a-python-list/" target="_blank" rel="noreferrer noopener">I’ve written a detailed article on this topic so check it out if you need more info.</a></p>
<p><strong>Efficient Method: </strong>A shorter and more concise way is to create a <a href="https://blog.finxter.com/python-dictionary/" target="_blank" rel="noreferrer noopener">dictionary </a>out of the elements in the list to remove all duplicates and convert the dictionary back to a list. This preserves the order of the original list elements.</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 = ['Alice', 'Bob', 'Bob', 1, 1, 1, 2, 3, 3]
print(list(dict.fromkeys(lst)))
# ['Alice', 'Bob', 1, 2, 3]</pre>
<ol>
<li>Convert the list to a dictionary with <code>dict.fromkeys(lst)</code>. </li>
<li>Convert the dictionary into a list with <code>list(dict)</code>.</li>
</ol>
<p>Each list element becomes a new <a href="https://blog.finxter.com/how-to-get-the-key-with-minimum-value-in-a-python-dictionary/" target="_blank" rel="noreferrer noopener">key to the dictionary</a>. For example, the list <code>[1, 2, 3]</code> becomes the dictionary <code>{1:None, 2:None, 3:None}</code>. All elements that occur multiple times will be assigned to the same key. Thus, the dictionary contains only unique keys—there cannot be multiple equal keys.</p>
<figure class="wp-block-image size-large is-resized"><img src="https://blog.finxter.com/wp-content/uploads/2020/04/removeDupsPython-1024x576.jpg" alt="" class="wp-image-7701" width="512" height="288" srcset="https://blog.finxter.com/wp-content/uploads/2020/04/removeDupsPython-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: 512px) 100vw, 512px" /></figure>
<p>As dictionary values, you take dummy values (per default).</p>
<p>Then, you convert the dictionary back to a list, throwing away the dummy values. </p>
<p>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, 1, 1, 3, 2, 5, 5, 2]
>>> dic = dict.fromkeys(lst)
>>> dic
{1: None, 3: None, 2: None, 5: None}
>>> duplicate_free = list(dic)
>>> duplicate_free
[1, 3, 2, 5]</pre>
<p>This way, you can simply use the ordered dictionary data type. </p>
<p><strong>Related blog articles:</strong></p>
<ul>
<li><a href="https://blog.finxter.com/how-to-remove-duplicates-from-a-python-list-of-lists/" target="_blank" rel="noreferrer noopener">Python Remove Duplicates From List of Lists</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-remove/" target="_blank">Python List Remove</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" target="_blank">The Ultimate Guide to Python Dictionaries!</a></li>
</ul>
<h2>Python List to Set Error: Unhashable Type List</h2>
<p>A common error is to try to convert a set to a list with unhashable data types. Here’s what happens if you try to convert a <a href="https://blog.finxter.com/python-list-of-lists/" target="_blank" rel="noreferrer noopener">list of lists</a> into a 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="">users = [['Alice', 23, 'female'], ['Bob', 26, 'male'], ['Ann', 31, 'female']] s = set(users)
print(s)</pre>
<p>The result is an error message <code>unhashable type: 'list'</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="">'''
Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 6, in &lt;module> s = set(users)
TypeError: unhashable type: 'list' '''</pre>
<p><strong>Why are lists unhashable?</strong></p>
<p>Because they are mutable: you can change a list by appending or removing elements. If you change the list data type, the hash value changes (it is calculated based on the content of the list). This directly violates the definition that a <em>“hash value […] never changes during its lifetime”</em> (<a href="https://blog.finxter.com/sets-in-python/" target="_blank" rel="noreferrer noopener">see here</a>).</p>
<p><strong>Key takeaway: mutable data types are not hashable. Therefore, you cannot use them in sets.</strong></p>
<p>So, how to solve it? Simply convert the inner lists into an immutable collection type such as a tuple:</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="">users = [('Alice', 23, 'female'), ('Bob', 26, 'male'), ('Ann', 31, 'female')] s = set(users)
print(s)
# {('Bob', 26, 'male'),
# ('Ann', 31, 'female'),
# ('Alice', 23, 'female')}</pre>
<p>Now, the result is a set of tuple elements. As tuples are immutable, the hash value will never change and you can create a set out of them.</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>


https://www.sickgaming.net/blog/2020/05/...ive-guide/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016