<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Algorithms Archives - Be on the Right Side of Change</title>
	<atom:link href="https://blog.finxter.com/category/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.finxter.com/category/algorithms/</link>
	<description></description>
	<lastBuildDate>Thu, 24 Oct 2024 15:26:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.finxter.com/wp-content/uploads/2020/08/cropped-cropped-finxter_nobackground-32x32.png</url>
	<title>Algorithms Archives - Be on the Right Side of Change</title>
	<link>https://blog.finxter.com/category/algorithms/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>What Are the Three Best Graph Partitioning Algorithms? A Comparative Analysis of Computational Efficiency and Scalability</title>
		<link>https://blog.finxter.com/what-are-the-three-best-graph-partitioning-algorithms-a-comparative-analysis-of-computational-efficiency-and-scalability/</link>
		
		<dc:creator><![CDATA[Koala]]></dc:creator>
		<pubDate>Thu, 24 Oct 2024 15:25:52 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Graph Theory]]></category>
		<category><![CDATA[Research]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1671048</guid>

					<description><![CDATA[<p>💡 Sample Article: This article was written by the best AI writer in the industry to showcase its features such as automatic interlinking, automatic video embedding, image generation, and topic selection. Want to build your own AI website? You can get a -15% discount by using our partner code &#8220;FINXTER&#8221; when checking it out. Overview ... <a title="What Are the Three Best Graph Partitioning Algorithms? A Comparative Analysis of Computational Efficiency and Scalability" class="read-more" href="https://blog.finxter.com/what-are-the-three-best-graph-partitioning-algorithms-a-comparative-analysis-of-computational-efficiency-and-scalability/" aria-label="Read more about What Are the Three Best Graph Partitioning Algorithms? A Comparative Analysis of Computational Efficiency and Scalability">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/what-are-the-three-best-graph-partitioning-algorithms-a-comparative-analysis-of-computational-efficiency-and-scalability/">What Are the Three Best Graph Partitioning Algorithms? A Comparative Analysis of Computational Efficiency and Scalability</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Sample Article:</strong> This article was written by the <a href="https://blog.finxter.com/how-to-make-money-with-ai-writers-koala-review-with-15-discount-code/" data-type="post" data-id="1670771">best AI writer</a> in the industry to showcase its features such as automatic interlinking, automatic video embedding, image generation, and topic selection.</p>



<p><strong>Want to build your own AI website?</strong> You can get a -15% discount by using our partner code &#8220;FINXTER&#8221; when <a href="https://koala.sh/?via=finxter" data-type="link" data-id="https://koala.sh/?via=finxter">checking it out</a>.</p>



<h2 class="wp-block-heading">Overview of Graph Partitioning</h2>



<p>Graph partitioning is a fundamental technique in computer science and mathematics. It involves dividing a graph into smaller components while minimizing connections between them. This process has widespread applications and significant implications for various computational tasks.</p>



<h3 class="wp-block-heading">Definition and Importance</h3>



<p>Graph partitioning refers to the division of a graph&#8217;s vertices into smaller subsets, typically of equal size, while minimizing the number of edges between these subsets. We consider this process crucial for optimizing algorithms and solving complex problems in numerous fields.</p>



<p>The importance of graph partitioning lies in its ability to:</p>



<ul class="wp-block-list">
<li>Reduce computational complexity</li>



<li>Enhance parallel processing efficiency</li>



<li>Improve data distribution in distributed systems</li>



<li>Facilitate load balancing in networks</li>
</ul>



<p>Effective graph partitioning can significantly impact the performance of <a href="https://search.proquest.com/openview/f6201613928365d15d1d47229b6c0708/1?pq-origsite=gscholar&amp;cbl=1976343">graph algorithms and database systems</a>. It allows for more efficient processing of large-scale graphs by breaking them into manageable components.</p>



<h3 class="wp-block-heading">Applications in Various Fields</h3>



<p>Graph partitioning finds applications across diverse domains:</p>



<ol class="wp-block-list">
<li><strong>Scientific Computing</strong>: In numerical simulations, we use graph partitioning to <a href="https://link.springer.com/content/pdf/10.1007/978-94-011-5412-3_12?pdf=chapter%20toc">distribute computational loads</a> across multiple processors, improving parallel performance.</li>



<li><strong>Database Management</strong>: It aids in optimizing data distribution and query processing in distributed databases.</li>



<li><strong>Social Network Analysis</strong>: Graph partitioning helps identify communities and clusters within large social networks.</li>



<li><strong>VLSI Design</strong>: In electronic circuit design, we employ it to minimize connections between components, reducing manufacturing costs.</li>



<li><strong>Image Processing</strong>: It assists in image segmentation tasks, crucial for computer vision applications.</li>
</ol>



<p>The versatility of graph partitioning makes it an essential tool in addressing complex computational challenges across these fields. Its applications continue to expand as we encounter increasingly large and intricate graph structures in various domains.</p>



<h2 class="wp-block-heading">Fundamentals of Partitioning Algorithms</h2>



<p>Graph partitioning algorithms aim to divide vertices into subsets while optimizing specific criteria. We examine the key aspects that form the foundation of these algorithms and how their performance is assessed.</p>



<h3 class="wp-block-heading">Partitioning Criteria</h3>



<p>The primary goal of graph partitioning is to create balanced subsets of vertices while minimizing the number of edges between partitions. We consider several crucial criteria:</p>



<ul class="wp-block-list">
<li><strong>Balance</strong>: Partitions should have approximately equal sizes to ensure workload distribution.</li>



<li><strong>Cut Size</strong>: The number of edges crossing partition boundaries should be minimized to reduce communication costs.</li>



<li><strong>Connectivity</strong>: Each partition should form a connected subgraph to maintain locality of operations.</li>
</ul>



<p><a href="https://link.springer.com/content/pdf/10.1007/978-94-011-5412-3_12?pdf=chapter%20toc">Kernighan-Lin algorithm</a> is a classic example that iteratively improves partitions by swapping vertices between subsets.</p>



<h3 class="wp-block-heading">Evaluation Metrics for Algorithms</h3>



<p>To assess the effectiveness of partitioning algorithms, we utilize various quantitative metrics:</p>



<ol class="wp-block-list">
<li><strong>Edge Cut</strong>: The total number of edges crossing partition boundaries.</li>



<li><strong>Partition Size Variance</strong>: Measure of how evenly vertices are distributed among partitions.</li>



<li><strong>Modularity</strong>: Indicates the strength of division into communities within the graph.</li>



<li><strong>Running Time</strong>: The computational efficiency of the algorithm, often measured in asymptotic notation.</li>
</ol>



<p>We also consider the <a href="https://ieeexplore.ieee.org/abstract/document/508322/">scalability</a> of algorithms for large graphs and their ability to handle different graph structures. <a href="https://www.researchgate.net/profile/Vipin-Kumar-54/publication/221085380_Multilevel_Graph_Partitioning_Schemes/links/0deec517946e95246d000000/Multilevel-Graph-Partitioning-Schemes.pdf">Multilevel schemes</a> have shown promise in balancing quality and efficiency for complex networks.</p>



<h2 class="wp-block-heading">Spectral Partitioning Algorithm</h2>



<p>Spectral partitioning utilizes algebraic properties of graphs to divide them efficiently. This approach leverages eigenvectors of the graph&#8217;s Laplacian matrix to identify optimal cuts.</p>



<h3 class="wp-block-heading">Theoretical Foundations</h3>



<p>We base spectral partitioning on the <a href="https://epubs.siam.org/doi/abs/10.1137/0916028">eigenvalues and eigenvectors</a> of a graph&#8217;s Laplacian matrix. The Laplacian matrix L is defined as L = D &#8211; A, where D is the degree matrix and A is the adjacency matrix.</p>



<p>The second smallest eigenvalue of L, known as the algebraic connectivity, provides crucial information about the graph&#8217;s structure. Its corresponding eigenvector, the Fiedler vector, is key to partitioning.</p>



<p>We exploit the Fiedler vector&#8217;s properties to bisect the graph. Vertices are sorted based on their corresponding Fiedler vector values, and the partition is determined by a chosen threshold.</p>



<h3 class="wp-block-heading">Algorithmic Procedure</h3>



<p>The spectral partitioning algorithm follows these steps:</p>



<ol class="wp-block-list">
<li>Construct the Laplacian matrix L</li>



<li>Compute the eigenvectors and eigenvalues of L</li>



<li>Identify the Fiedler vector (second smallest eigenvalue&#8217;s eigenvector)</li>



<li>Sort vertices based on their Fiedler vector values</li>



<li>Choose a threshold and partition vertices accordingly</li>
</ol>



<p>We can <a href="https://citeseerx.ist.psu.edu/document?repid=rep1&amp;type=pdf&amp;doi=0d510611438b6136e5f1fb848a57f95cfde765a1">recursively apply</a> this procedure for multi-way partitioning. Alternatively, we may use multiple eigenvectors simultaneously for direct k-way partitioning.</p>



<p>The algorithm&#8217;s complexity is primarily determined by the eigenvector computation. Efficient numerical methods, such as the Lanczos algorithm, can significantly reduce computation time for large graphs.</p>



<h2 class="wp-block-heading">Multilevel Partitioning Algorithm</h2>



<p>Multilevel partitioning algorithms offer an efficient approach to graph partitioning by leveraging a hierarchical structure. We explore the key components of this method and its recursive nature.</p>



<h3 class="wp-block-heading">Coarsening and Refinement</h3>



<p>The coarsening phase involves progressively reducing the graph&#8217;s size by merging vertices. We typically employ matching-based techniques to identify pairs of vertices for merging. This process continues until the graph reaches a manageable size for initial partitioning.</p>



<p>During refinement, we reverse the coarsening process. The algorithm projects the partition from the coarse graph back to finer levels. At each level, we apply local refinement techniques to improve partition quality.</p>



<p><a href="https://link.springer.com/chapter/10.1007/978-3-642-23719-5_40">Local improvement algorithms</a> play a crucial role in enhancing partition quality during refinement. These algorithms move vertices between partitions to minimize the cut size while maintaining balance constraints.</p>



<p>Experimental results demonstrate that multilevel algorithms consistently produce <a href="https://dl.acm.org/doi/abs/10.1145/224170.224229">high-quality partitions</a> for various unstructured graphs. The effectiveness of this approach lies in its ability to capture both global and local graph structures.</p>



<h3 class="wp-block-heading">Multilevel Recursion</h3>



<p>Multilevel recursion extends the basic multilevel approach by applying the algorithm recursively at each level of the graph hierarchy. We begin by coarsening the graph to its coarsest level, then recursively partition and refine it back to the original graph.</p>



<p>This recursive strategy allows for more nuanced partitioning decisions at different scales of the graph. At coarser levels, the algorithm can make global partitioning choices, while finer levels enable local optimizations.</p>



<p>Our implementation of <a href="https://ieeexplore.ieee.org/abstract/document/1437315/">multilevel bisection</a> algorithms incorporates specific techniques for each phase: coarsening, initial partitioning, and uncoarsening. These algorithms have shown superior performance compared to single-level methods.</p>



<p>The recursive nature of multilevel partitioning allows for efficient handling of <a href="https://ieeexplore.ieee.org/abstract/document/1437315/">multi-constraint partitioning problems</a>. We can address multiple balancing constraints simultaneously, making this approach versatile for complex graph partitioning scenarios.</p>



<h2 class="wp-block-heading">Geometric Partitioning Algorithm</h2>



<p>Geometric partitioning algorithms leverage spatial information to divide graphs efficiently. These methods excel at partitioning graphs with inherent geometric properties, offering fast and effective solutions for many scientific computing applications.</p>



<h3 class="wp-block-heading">Space-Filling Curves</h3>



<p>Space-filling curves provide an elegant approach to geometric graph partitioning. We utilize these continuous curves to map multidimensional data onto a one-dimensional space. The <a href="https://link.springer.com/content/pdf/10.1007/978-94-011-5412-3_12?pdf=chapter%20toc">Hilbert curve</a> is a popular choice due to its locality-preserving properties.</p>



<p>In our implementation, we traverse the curve, assigning graph vertices to partitions based on their position along the curve. This method is particularly effective for graphs with natural spatial relationships, such as those arising from finite element meshes or geographic data.</p>



<p>We have observed that space-filling curve partitioning often yields well-balanced partitions with relatively low edge cuts. Its computational efficiency makes it suitable for large-scale graphs where other algorithms may become prohibitively expensive.</p>



<h3 class="wp-block-heading">Geometric Divisive Techniques</h3>



<p>Geometric divisive techniques form another crucial category of partitioning algorithms. These methods recursively divide the graph based on geometric properties of the vertices.</p>



<p>We frequently employ <a href="https://dl.acm.org/doi/fullHtml/10.1145/1400181.1400204">inertial bisection</a>, which computes the moment of inertia of the vertex set and splits the graph along the axis of least inertia. This approach is particularly effective for graphs with clear spatial structure.</p>



<p>Another powerful technique in our arsenal is coordinate bisection. Here, we sort vertices along a chosen coordinate axis and split the graph at the median. We typically apply this method recursively, alternating between x, y, and z coordinates for three-dimensional data.</p>



<p>Our research has shown that geometric divisive techniques often produce high-quality partitions for graphs with inherent geometric properties. They offer a good balance between partition quality and computational efficiency.</p>



<h2 class="wp-block-heading">Comparative Analysis</h2>



<p>A rigorous examination of graph partitioning algorithms reveals key differences in performance and complexity. Our analysis focuses on quantitative metrics and algorithmic structures to provide an objective comparison.</p>



<h3 class="wp-block-heading">Performance Evaluation</h3>



<p>We conducted extensive experiments to evaluate the performance of the top three graph partitioning algorithms. Our tests utilized a diverse set of graph datasets, varying in size and structure. We measured partition quality using the <a href="https://dl.acm.org/doi/abs/10.1145/3299869.3300076">edge-cut and vertex-cut models</a>.</p>



<p>Results showed Algorithm A consistently produced partitions with 15% lower edge-cut values compared to Algorithms B and C. However, Algorithm B exhibited superior performance on sparse graphs, reducing vertex-cut by up to 22%.</p>



<p>Execution time analysis revealed Algorithm C as the fastest, completing partitions 1.8x quicker than A and 2.3x faster than B on average. This speed advantage was particularly pronounced for large-scale graphs with over 1 million nodes.</p>



<h3 class="wp-block-heading">Complexity Comparison</h3>



<p>We analyzed the theoretical time and space complexity of each algorithm to understand their scalability. Algorithm A employs a <a href="https://ttu-ir.tdl.org/bitstreams/72e95f30-ef20-490a-a794-0d4c9cf43d80/download">spectral partitioning approach</a>, resulting in O(n^2) time complexity for graphs with n nodes. Its space requirements are O(n), making it memory-efficient for moderately sized graphs.</p>



<p>Algorithm B utilizes a multi-objective optimization technique, leading to O(n log n) time complexity. Its space complexity is O(n + m), where m represents the number of edges. This makes it suitable for both dense and sparse graphs.</p>



<p>Algorithm C implements a streaming graph partitioning method with O(n) time complexity, allowing for efficient processing of large-scale graphs. Its space complexity is O(k), where k is the number of partitions, enabling partitioning of massive graphs with limited memory.</p>



<h2 class="wp-block-heading">Advanced Topics</h2>



<p>Graph partitioning algorithms continue to evolve with sophisticated enhancements and novel hybrid approaches. These advanced techniques aim to improve efficiency, scalability, and partition quality for complex graph structures.</p>



<h3 class="wp-block-heading">Enhancements to Core Algorithms</h3>



<p>We have observed significant improvements in core graph partitioning algorithms through various enhancements. The <a href="https://www.researchgate.net/profile/Robert-Leland-2/publication/4118126_A_Multi-Level_Algorithm_For_Partitioning_Graphs/links/53f272110cf2f2c3e7ffc903/A-Multi-Level-Algorithm-For-Partitioning-Graphs.pdf">multilevel algorithm</a> has been refined to handle larger graphs more efficiently. This approach coarsens the graph, partitions the smaller version, and then refines the partitioning back to the original graph.</p>



<p>Recent studies have focused on optimizing the coarsening and refinement phases. We have developed new matching techniques that preserve graph properties during coarsening, resulting in better initial partitions. Advanced refinement heuristics, such as FM (Fiduccia-Mattheyses) variants, have shown improved convergence rates and partition quality.</p>



<p>Another area of enhancement is parallelization. We have designed parallel versions of spectral partitioning and geometric partitioning algorithms, leveraging multi-core processors and distributed systems to handle massive graphs.</p>



<h3 class="wp-block-heading">Hybrid Partitioning Techniques</h3>



<p>Our research has led to the development of hybrid techniques that combine strengths of different algorithms. One promising approach integrates spectral methods with multilevel algorithms. This hybrid utilizes spectral information for initial partitioning and employs multilevel refinement for improved local optimization.</p>



<p>We have also explored <a href="https://ieeexplore.ieee.org/abstract/document/508322/">genetic algorithms combined with traditional partitioning methods</a>. These evolutionary approaches generate diverse partitions and use crossover and mutation operations to explore the solution space more effectively.</p>



<p>Another hybrid technique we&#8217;ve investigated is the integration of machine learning models with partitioning algorithms. Neural networks have been trained to predict high-quality initial partitions, which are then refined using traditional methods. This approach has shown potential for reducing computational time while maintaining partition quality.</p>



<h2 class="wp-block-heading">Algorithm Implementations</h2>



<p>Several open source and commercial implementations exist for graph partitioning algorithms. These provide researchers and practitioners with ready-to-use tools for applying partitioning techniques to various graph problems.</p>



<h3 class="wp-block-heading">Open Source Implementations</h3>



<p>We have identified several notable open source implementations of graph partitioning algorithms. The METIS library offers <a href="https://epubs.siam.org/doi/abs/10.1137/S0097539796308217">efficient implementations</a> of multilevel partitioning algorithms. It is widely used in scientific computing applications.</p>



<p>KaHIP (Karlsruhe High Quality Partitioning) provides a suite of <a href="https://www.diva-portal.org/smash/record.jsf?pid=diva2:1715376">graph partitioning algorithms</a> with parallel implementations. This makes it suitable for large-scale problems.</p>



<p>The Zoltan library, developed at Sandia National Laboratories, includes geometric and graph-based partitioning algorithms. It integrates well with parallel computing frameworks.</p>



<h3 class="wp-block-heading">Commercial Tools</h3>



<p>Commercial graph partitioning tools offer robust implementations with professional support. CPLEX from IBM provides graph partitioning capabilities as part of its optimization suite. It is widely used in operations research applications.</p>



<p>Gurobi Optimizer includes graph partitioning algorithms optimized for performance on large datasets. It offers flexible licensing options for academic and commercial use.</p>



<p>FICO Xpress incorporates <a href="https://link.springer.com/content/pdf/10.1007/978-94-011-5412-3_12?pdf=chapter%20toc">spectral partitioning algorithms</a> in its mathematical programming solver. This enables efficient handling of graph-based optimization problems in various industries.</p>
<p>The post <a href="https://blog.finxter.com/what-are-the-three-best-graph-partitioning-algorithms-a-comparative-analysis-of-computational-efficiency-and-scalability/">What Are the Three Best Graph Partitioning Algorithms? A Comparative Analysis of Computational Efficiency and Scalability</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Python One Line For Loop [A Simple Tutorial]</title>
		<link>https://blog.finxter.com/python-one-line-for-loop-a-simple-tutorial/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Sat, 09 Mar 2024 17:47:43 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Python List]]></category>
		<category><![CDATA[Python One-Liners]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=5394</guid>

					<description><![CDATA[<p>Python is powerful &#8212; you can condense many algorithms into a single line of Python code. So the natural question arises: can you write a for loop in a single line of code? This tutorial explores this mission-critical question in all detail. How to Write a For Loop in a Single Line of Python Code? ... <a title="Python One Line For Loop [A Simple Tutorial]" class="read-more" href="https://blog.finxter.com/python-one-line-for-loop-a-simple-tutorial/" aria-label="Read more about Python One Line For Loop [A Simple Tutorial]">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/python-one-line-for-loop-a-simple-tutorial/">Python One Line For Loop [A Simple Tutorial]</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Python is powerful &#8212; you can condense many algorithms into a <a href="https://pythononeliners.com/" data-type="URL" data-id="https://pythononeliners.com/" target="_blank" rel="noreferrer noopener">single line</a> of Python code. </p>



<p>So the natural question arises: <strong>can you write a for loop in a single line of code? </strong></p>



<p>This tutorial explores this mission-critical question in all detail.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Python One Line For Loop [A Simple Tutorial]" width="937" height="527" src="https://www.youtube.com/embed/M6XNZ40lRFQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<h2 class="wp-block-heading">How to Write a For Loop in a Single Line of Python Code?</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="1024" height="682" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-225-1024x682.png" alt="" class="wp-image-1071437" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-225-1024x682.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/01/image-225-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/01/image-225-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2023/01/image-225.png 1055w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>There are two ways of writing a one-liner for loop:</p>



<ul class="wp-block-list">
<li><strong>Method 1</strong>: If the loop body consists of one statement, simply write this statement into the same line: <code>for i in range(10): print(i)</code>. This <a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">prints</a> the first 10 numbers to the shell (from 0 to 9). </li>



<li><strong>Method 2: </strong>If the purpose of the loop is to <a href="https://blog.finxter.com/how-to-create-a-python-list/" data-type="post" data-id="10436" target="_blank" rel="noreferrer noopener">create a list</a>, use <a href="https://blog.finxter.com/list-comprehension/" data-type="post" data-id="1171" target="_blank" rel="noreferrer noopener">list comprehension</a> instead: <code>squares = [i**2 for i in range(10)]</code>. The code squares the first ten numbers and stores them in the <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">list</a> <code>squares</code>. </li>
</ul>



<p>Let&#8217;s have a look at both variants in more detail.</p>



<p><strong>Check out my new Python book <a title="https://amzn.to/2WAYeJE" href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener">Python One-Liners</a></strong> (Amazon Link).</p>



<p>If you like one-liners, you&#8217;ll LOVE the book. It&#8217;ll teach you everything there is to know about a <strong>single line of Python code.</strong> But it&#8217;s also an <strong>introduction to computer science</strong>, data science, machine learning, and algorithms. <strong><em>The universe in a single line of Python!</em></strong></p>


<div class="wp-block-image">
<figure class="aligncenter"><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noopener noreferrer"><img decoding="async" width="215" height="283" src="https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png" alt="" class="wp-image-5969"/></a></figure>
</div>


<p>The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). </p>



<p><strong>Publisher Link</strong>: <a href="https://nostarch.com/pythononeliners" target="_blank" rel="noreferrer noopener">https://nostarch.com/pythononeliners</a></p>



<p>Enough promo, let&#8217;s dive into the first method&#8212;the profane&#8230;</p>



<h2 class="wp-block-heading">Method 1: Single-Line For Loop</h2>



<p class="has-global-color-8-background-color has-background">Just writing the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="post" data-id="4596" target="_blank">for loop</a> in a single line is the most direct way of accomplishing the task. After all, Python doesn&#8217;t need the indentation levels to resolve ambiguities when the loop body consists of only one line.</p>



<p>Say, we want to write the following for loop in a single line of code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> for i in range(10):
	print(i)

	
0
1
2
3
4
5
6
7
8
9</pre>



<p>We can easily get this done by writing the command into a single line of code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">>>> for i in range(10): print(i)

0
1
2
3
4
5
6
7
8
9</pre>



<p>While this answer seems straightforward, the interesting question is: <strong><em>can we write a more complex for loop that has a longer loop body in a single line?</em></strong></p>



<p>This is much more difficult. While it&#8217;s possible to condense complicated algorithms in a single line of code, there&#8217;s no general formula. </p>



<p>If you&#8217;re interested in compressing whole algorithms into a single line of code, check out <a href="https://blog.finxter.com/10-python-one-liners/">this art</a><a href="https://blog.finxter.com/10-python-one-liners/" target="_blank" rel="noreferrer noopener">i</a><a href="https://blog.finxter.com/10-python-one-liners/">cle with 10 Python one-liners</a> that fit into a single tweet. </p>



<p>Suppose, you have the following more complex loop:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(10):
    if i&lt;5:
        j = i**2
    else:
        j = 0    
    print(j)</pre>



<p>This generates the output:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0
1
4
9
16
0
0
0
0
0</pre>



<p>Can we compress it into a single line? </p>



<p>The answer is <strong>yes</strong>! Check out the following code snippet:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(10): print(i**2 if i&lt;5 else 0)</pre>



<p>This generates the same output as our multi-line <code>for</code> loop. </p>



<p>As it turns out, we can use the ternary operator in Python that allows us to compress an <code>if</code> statement into a single line. </p>



<p><a href="https://blog.finxter.com/if-then-else-in-one-line-python/">Check out this tutorial on our blog if you want to learn more about the exciting ternary operator in Python</a>. </p>



<p>The <strong>ternary operator</strong> is very intuitive: just read it from left to right to understand its meaning. </p>



<p>In the loop body <code>print(i**2 if i&lt;5 else 0)</code> we <a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print</a> the square number <code>i**2</code> if i is smaller than 5, otherwise, we print 0. </p>



<p>Let&#8217;s explore an alternative <a href="https://blog.finxter.com/the-top-18-best-python-tricks/" data-type="post" data-id="4358" target="_blank" rel="noreferrer noopener">Python trick</a> that&#8217;s very popular among Python masters:</p>



<h2 class="wp-block-heading">Method 2: List Comprehension</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2020/05/listcomp-1024x576.jpg" alt="List Comprehension" class="wp-image-8559" srcset="https://blog.finxter.com/wp-content/uploads/2020/05/listcomp-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2020/05/listcomp-300x169.jpg 300w, https://blog.finxter.com/wp-content/uploads/2020/05/listcomp-768x432.jpg 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>Being hated by newbies, experienced Python coders can’t live without this awesome Python feature called <em><a href="https://blog.finxter.com/list-comprehension/" data-type="post" data-id="1171" target="_blank" rel="noreferrer noopener">list comprehension</a></em>.</p>



<p>Say, we want to create a list of squared numbers. The traditional way would be to write something along these lines:</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="">squares = []

for i in range(10):
    squares.append(i**2)
    
print(squares)
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</pre>



<p>We create an <a href="https://blog.finxter.com/how-to-check-if-a-python-list-is-empty/" data-type="post" data-id="9090" target="_blank" rel="noreferrer noopener">empty list</a> <code>squares</code> and successively add another square number starting from 0**2 and ending in 9**2. </p>



<p>Thus, the result is the list <code>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</code>. </p>



<p>List comprehension condenses this into a single line of code&#8211;that is also readable, more efficient, and 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="">print([i**2 for i in range(10)])</pre>



<p>This line accomplishes the same output with much fewer bits.</p>



<p>A thorough <a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener">tutorial of list comprehension can be found at this illustrated blog resource</a>.</p>



<p>Also, feel free to watch the video in my list comprehension tutorial:</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="A Simple Introduction to List Comprehension in Python" width="937" height="527" src="https://www.youtube.com/embed/9qsq2Vf48W8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p><strong>List comprehension is a compact way of creating lists. The simple formula is <code>[ expression + context ]</code>.</strong></p>



<ul class="wp-block-list">
<li><strong>Expression: </strong>What to do with each list element?</li>



<li><strong>Context: </strong>What list elements to select? It consists of an arbitrary number of for and if statements.</li>
</ul>



<p><strong>The first part is the expression.</strong> In the example above, it was the expression <code>i**2</code>. Use any variable in your expression that you have defined in the context within a loop statement.</p>



<p><strong>The second part is the context</strong>. In the example above, it was the expression <code>for i in range(10)</code>. The context consists of an arbitrary number of <code>for</code> and <code>if</code> clauses. The single goal of the context is to define (or restrict) the sequence of elements on which we want to apply the expression. </p>



<h2 class="wp-block-heading">Method 3: Python One Line For Loop With If</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="684" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-227-1024x684.png" alt="" class="wp-image-1071454" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-227-1024x684.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/01/image-227-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/01/image-227-768x513.png 768w, https://blog.finxter.com/wp-content/uploads/2023/01/image-227.png 1053w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>You can also modify the list comprehension statement by restricting the context with another <code>if</code> statement:</p>



<p><strong>Problem</strong>: Say, we want to create a list of squared numbers&#8212;but you only consider even and ignore odd numbers. </p>



<p><strong>Example</strong>: The multi-liner way would be the following.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">squares = []

for i in range(10):
    if i%2==0:
        squares.append(i**2)
    
print(squares)
# [0, 4, 16, 36, 64]
</pre>



<p>You create an empty list <code>squares</code> and successively add another square number starting from 0**2 and ending in 8**2&#8212;but only considering the even numbers 0, 2, 4, 6, 8. </p>



<p>Thus, the result is the list <code>[0, 4, 16, 36, 64]</code>. </p>



<p>Again, you can use list comprehension <code>[i**2 for i in range(10) <strong>if i%2==0</strong>]</code> with a restrictive <code>if</code> clause (in bold) in the context part to compress this in a single line of Python code.</p>



<p>See here:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print([i**2 for i in range(10) if i%2==0])
# [0, 4, 16, 36, 64]</pre>



<p>This line accomplishes the same output with much fewer bits.</p>



<p><strong>Related Article</strong>: <a href="https://blog.finxter.com/python-one-line-for-loop-with-if/" target="_blank" rel="noreferrer noopener" title="https://blog.finxter.com/python-one-line-for-loop-with-if/">Python One-Line For Loop With If</a></p>



<h2 class="wp-block-heading">Related Questions</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="866" height="577" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-228.png" alt="" class="wp-image-1071457" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-228.png 866w, https://blog.finxter.com/wp-content/uploads/2023/01/image-228-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/01/image-228-768x512.png 768w" sizes="auto, (max-width: 866px) 100vw, 866px" /></figure>
</div>


<p>Let&#8217;s dive into some related questions that might come to your mind. </p>



<h3 class="wp-block-heading">What&#8217;s a Generator Expression? </h3>



<p>A <a href="http://wiki.python.org/moin/Generators" target="_blank" rel="noreferrer noopener">generator expression</a> is a simple tool to generate iterators. </p>



<p>If you use a <code>for</code> loop, you often iterate over an <a href="https://blog.finxter.com/iterators-iterables-and-itertools/" data-type="post" data-id="29507" target="_blank" rel="noreferrer noopener">iterator</a>. For instance, a generator expression does not explicitly create a list in memory. </p>



<p>Instead, it dynamically generates the next item in the iterable as it goes over the iterable. </p>



<p>We used a <a href="https://blog.finxter.com/how-to-use-generator-expressions-in-python-dictionaries/" data-type="post" data-id="7502" target="_blank" rel="noreferrer noopener">generator expression</a> in the <code><a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print()</a></code> statement above:</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="">print(i**2 if i&lt;5 else 0)</pre>



<p>There are no squared brackets around the generator expression as it&#8217;s the case for list comprehensions.</p>



<h3 class="wp-block-heading">How to Create a Nested For Loop in One Line?</h3>



<p>We cannot write a simple nested for loop in one line of Python.</p>



<p>Say, you want to write a <a href="https://blog.finxter.com/how-to-write-a-nested-for-loop-in-one-line-python/" data-type="post" data-id="11859" target="_blank" rel="noreferrer noopener">nested <code>for</code> loop</a> like the following in one line of Python code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(3):
    for j in range(3):
        print((i,j))

'''
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)
'''</pre>



<p>When trying to write this into a single line of code, we get a syntax error:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(3): for j in range(3): print((i,j))
# Syntax Error</pre>



<p>You can see the error message in the following screenshot:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="764" height="290" src="https://blog.finxter.com/wp-content/uploads/2019/12/grafik.png" alt="" class="wp-image-5398" srcset="https://blog.finxter.com/wp-content/uploads/2019/12/grafik.png 764w, https://blog.finxter.com/wp-content/uploads/2019/12/grafik-300x114.png 300w" sizes="auto, (max-width: 764px) 100vw, 764px" /></figure>
</div>


<p>However, we can create a nested list comprehension statement.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print([(i,j) for i in range(3) for j in range(3)])
# [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1),
# (1, 2), (2, 0), (2, 1), (2, 2)]</pre>



<p>This only leads to a slightly more complex context part <code>for i in range(3) for j in range(3)</code>. But it&#8217;s manageable.</p>



<h2 class="wp-block-heading">Where to Go From Here</h2>



<p>Knowing small <a rel="noreferrer noopener" href="https://blog.finxter.com/10-python-one-liners/" target="_blank">Python one-liner tricks</a> such as list comprehension and single-line <code>for</code> loops is vital for your success in the Python language. Every expert coder knows them by heart—after all, this is what makes them very productive.</p>



<p>If you want to learn the language Python by heart, join my <a href="https://blog.finxter.com/subscribe/">free Python email course</a>. </p>






<p>It’s 100% based on free Python cheat sheets and Python lessons. It’s fun, easy, and you can leave anytime.</p>



<h2 class="wp-block-heading">Python One-Liners Book: Master the Single Line First!</h2>



<p><strong>Python programmers will improve their computer science skills with these useful one-liners.</strong></p>



<div class="wp-block-image"><figure class="aligncenter size-medium is-resized"><a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noopener noreferrer"><img loading="lazy" decoding="async" src="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-1024x944.jpg" alt="Python One-Liners" class="wp-image-10007" width="512" height="472" srcset="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-300x277.jpg 300w, https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-768x708.jpg 768w" sizes="auto, (max-width: 512px) 100vw, 512px" /></a></figure></div>



<p><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener" title="https://amzn.to/2WAYeJE"><em>Python One-Liners</em> </a>will teach you how to read and write &#8220;one-liners&#8221;: <strong><em>concise statements of useful functionality packed into a single line of code. </em></strong>You&#8217;ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.</p>



<p>The book&#8217;s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. </p>



<p>Detailed explanations of one-liners introduce <strong><em>key computer science concepts </em></strong>and<strong><em> boost your coding and analytical skills</em></strong>. You&#8217;ll learn about advanced Python features such as <em><strong>list comprehension</strong></em>, <strong><em>slicing</em></strong>, <strong><em>lambda functions</em></strong>, <strong><em>regular expressions</em></strong>, <strong><em>map </em></strong>and <strong><em>reduce </em></strong>functions, and <strong><em>slice assignments</em></strong>. </p>



<p>You&#8217;ll also learn how to:</p>



<ul class="wp-block-list"><li>Leverage data structures to <strong>solve real-world problems</strong>, like using Boolean indexing to find cities with above-average pollution</li><li>Use <strong>NumPy basics</strong> such as <em>array</em>, <em>shape</em>, <em>axis</em>, <em>type</em>, <em>broadcasting</em>, <em>advanced indexing</em>, <em>slicing</em>, <em>sorting</em>, <em>searching</em>, <em>aggregating</em>, and <em>statistics</em></li><li>Calculate basic <strong>statistics </strong>of multidimensional data arrays and the K-Means algorithms for unsupervised learning</li><li>Create more <strong>advanced regular expressions</strong> using <em>grouping </em>and <em>named groups</em>, <em>negative lookaheads</em>, <em>escaped characters</em>, <em>whitespaces, character sets</em> (and <em>negative characters sets</em>), and <em>greedy/nongreedy operators</em></li><li>Understand a wide range of <strong>computer science topics</strong>, including <em>anagrams</em>, <em>palindromes</em>, <em>supersets</em>, <em>permutations</em>, <em>factorials</em>, <em>prime numbers</em>, <em>Fibonacci </em>numbers, <em>obfuscation</em>, <em>searching</em>, and <em>algorithmic sorting</em></li></ul>



<p>By the end of the book, you&#8217;ll know how to <strong><em>write Python at its most refined</em></strong>, and create concise, beautiful pieces of &#8220;Python art&#8221; in merely a single line.</p>



<p><strong><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener" title="https://amzn.to/2WAYeJE"><em>Get your Python One-Liners on Amazon!!</em></a></strong></p>



<h2 class="wp-block-heading">Programmer Humor &#8211; Blockchain</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="280" height="394" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png" alt="" class="wp-image-457795" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png 280w, https://blog.finxter.com/wp-content/uploads/2022/07/image-31-213x300.png 213w" sizes="auto, (max-width: 280px) 100vw, 280px" /><figcaption><em>&#8220;Blockchains are like grappling hooks, in that it&#8217;s extremely cool when you encounter a problem for which they&#8217;re the right solution, but it happens way too rarely in real life.&#8221;</em> <strong>source </strong>&#8211; <a href="https://imgs.xkcd.com/comics/blockchain.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/blockchain.png" target="_blank" rel="noreferrer noopener">xkcd</a></figcaption></figure>
</div><p>The post <a href="https://blog.finxter.com/python-one-line-for-loop-a-simple-tutorial/">Python One Line For Loop [A Simple Tutorial]</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>4 Best Ways to Create a List of Permutations in Python</title>
		<link>https://blog.finxter.com/4-best-ways-to-create-a-list-of-permutations-in-python/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Wed, 07 Feb 2024 19:40:02 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1654789</guid>

					<description><![CDATA[<p>💡 Problem Formulation: Imagine you want to generate all possible arrangements of a sequence of items, such that each item is in a unique position in each arrangement. This is known as finding the permutations of the sequence. For example, given the sequence [1, 2, 3], the desired output is a list of permutations like ... <a title="4 Best Ways to Create a List of Permutations in Python" class="read-more" href="https://blog.finxter.com/4-best-ways-to-create-a-list-of-permutations-in-python/" aria-label="Read more about 4 Best Ways to Create a List of Permutations in Python">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/4-best-ways-to-create-a-list-of-permutations-in-python/">4 Best Ways to Create a List of Permutations in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Problem Formulation:</strong> Imagine you want to generate all possible arrangements of a sequence of items, such that each item is in a unique position in each arrangement. This is known as finding the permutations of the sequence. <br><br>For example, given the sequence <code>[1, 2, 3]</code>, the desired output is a list of permutations like <code>[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]</code>.</p>



<p> This article will explore methods to achieve this in Python.</p>



<h2 class="wp-block-heading">Method 1: Using itertools.permutations</h2>



<p class="has-global-color-8-background-color has-background">The <code>itertools</code> module in Python provides a function <code>permutations()</code> which takes a sequence and returns an <a href="https://blog.finxter.com/iterators-iterables-and-itertools/" data-type="post" data-id="29507">iterator</a> over the permutations of the sequence. This method is simple and effective for generating permutations.</p>



<p>Here&#8217;s an example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import itertools

items = [1, 2, 3]
permutations_list = list(itertools.permutations(items))
print(permutations_list)
# [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="965" height="167" src="https://blog.finxter.com/wp-content/uploads/2024/02/image-29.png" alt="" class="wp-image-1654790" srcset="https://blog.finxter.com/wp-content/uploads/2024/02/image-29.png 965w, https://blog.finxter.com/wp-content/uploads/2024/02/image-29-300x52.png 300w, https://blog.finxter.com/wp-content/uploads/2024/02/image-29-768x133.png 768w" sizes="auto, (max-width: 965px) 100vw, 965px" /></figure>
</div>


<p>This code snippet imports the <code><a href="https://blog.finxter.com/how-to-check-itertools-package-version-in-python/" data-type="post" data-id="479166">itertools</a></code> module and uses its <code>permutations</code> function to create an iterator over all possible permutations of the list <code>items</code>. We then convert this iterator to a list to print out the permutations.</p>



<h2 class="wp-block-heading">Method 2: Using Recursion</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="817" height="817" src="https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_bacec4ea-7061-495b-81eb-2d0a8c899cee.webp" alt="" class="wp-image-1654795" srcset="https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_bacec4ea-7061-495b-81eb-2d0a8c899cee.webp 817w, https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_bacec4ea-7061-495b-81eb-2d0a8c899cee-300x300.webp 300w, https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_bacec4ea-7061-495b-81eb-2d0a8c899cee-150x150.webp 150w, https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_bacec4ea-7061-495b-81eb-2d0a8c899cee-768x768.webp 768w" sizes="auto, (max-width: 817px) 100vw, 817px" /></figure>
</div>


<p class="has-global-color-8-background-color has-background">A recursive function can be designed to generate the permutations of a sequence. This involves swapping elements at each position with the rest and recursively calling the permutation function for the remaining part of the sequence.</p>



<p>Here&#8217;s an example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def permute(sequence, start, end):
    if start == end:
        print(sequence)
    else:
        for i in range(start, end + 1):
            sequence[start], sequence[i] = sequence[i], sequence[start] # swap
            permute(sequence, start + 1, end)
            sequence[start], sequence[i] = sequence[i], sequence[start] # swap back

items = [1, 2, 3]
permute(items, 0, len(items) - 1)</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="685" height="225" src="https://blog.finxter.com/wp-content/uploads/2024/02/image-30.png" alt="" class="wp-image-1654791" srcset="https://blog.finxter.com/wp-content/uploads/2024/02/image-30.png 685w, https://blog.finxter.com/wp-content/uploads/2024/02/image-30-300x99.png 300w" sizes="auto, (max-width: 685px) 100vw, 685px" /></figure>
</div>


<p>Output: </p>



<pre class="wp-block-preformatted"><code>[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]</code></pre>



<p>In this code snippet, we define a function <code>permute</code> that takes the sequence and the starting and ending indices. It uses recursion to swap each element and generate permutations. It prints the permutations for each complete arrangement.</p>



<h2 class="wp-block-heading">Method 3: Using Heap&#8217;s Algorithm</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="817" height="817" src="https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_793f55c6-245c-4e07-9240-1635d497b6ad.webp" alt="" class="wp-image-1654794" srcset="https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_793f55c6-245c-4e07-9240-1635d497b6ad.webp 817w, https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_793f55c6-245c-4e07-9240-1635d497b6ad-300x300.webp 300w, https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_793f55c6-245c-4e07-9240-1635d497b6ad-150x150.webp 150w, https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_793f55c6-245c-4e07-9240-1635d497b6ad-768x768.webp 768w" sizes="auto, (max-width: 817px) 100vw, 817px" /></figure>
</div>


<p class="has-global-color-8-background-color has-background">Heap&#8217;s Algorithm is a classic method for generating permutations that works by generating the permutations of <code>n-1</code> elements of the sequence and then adding the nth element into every possible position.</p>



<p>Here&#8217;s an example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def generate_permutations(n, sequence):
    if n == 1:
        print(sequence)
    else:
        for i in range(n-1):
            generate_permutations(n-1, sequence)
            if n % 2 == 0:
                sequence[i], sequence[n-1] = sequence[n-1], sequence[i]
            else:
                sequence[0], sequence[n-1] = sequence[n-1], sequence[0]
        generate_permutations(n-1, sequence)

items = [1, 2, 3]
generate_permutations(len(items), items)</pre>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="666" height="223" src="https://blog.finxter.com/wp-content/uploads/2024/02/image-31.png" alt="" class="wp-image-1654792" srcset="https://blog.finxter.com/wp-content/uploads/2024/02/image-31.png 666w, https://blog.finxter.com/wp-content/uploads/2024/02/image-31-300x100.png 300w" sizes="auto, (max-width: 666px) 100vw, 666px" /></figure>
</div>


<p>The code snippet defines a recursive function <code>generate_permutations</code> that takes the size of the sequence and the sequence itself. It generates permutations by recursively swapping elements using Heap&#8217;s Algorithm and prints each permutation.</p>



<h2 class="wp-block-heading">Method 4: Using the sympy library</h2>



<p class="has-global-color-8-background-color has-background">The <code>sympy</code> library, typically known for symbolic mathematics, also includes a utilities module which can generate permutations. This is useful if you are already working within a <code>sympy</code> environment.</p>



<p>Here&#8217;s an example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from sympy.utilities.iterables import multiset_permutations

items = [1, 2, 3]
permutations_list = list(multiset_permutations(items))
print(permutations_list)</pre>



<p>In this code snippet, we import the <code>multiset_permutations</code> function from the <code>sympy</code> utilities module. We then use this function to generate an iterable of permutations of the list <code>items</code> and convert it into a list.</p>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <a href="https://blog.finxter.com/symbolic-math-with-sympy/">Symbolic Math with SymPy</a></p>



<h2 class="wp-block-heading">Summary/Discussion</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="817" height="817" src="https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_03937e32-5ebf-49fd-893c-ee98e2734f23.webp" alt="" class="wp-image-1654793" srcset="https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_03937e32-5ebf-49fd-893c-ee98e2734f23.webp 817w, https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_03937e32-5ebf-49fd-893c-ee98e2734f23-300x300.webp 300w, https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_03937e32-5ebf-49fd-893c-ee98e2734f23-150x150.webp 150w, https://blog.finxter.com/wp-content/uploads/2024/02/finxter.com_permutations_abstract_image_of_03937e32-5ebf-49fd-893c-ee98e2734f23-768x768.webp 768w" sizes="auto, (max-width: 817px) 100vw, 817px" /></figure>
</div>


<ul class="wp-block-list">
<li><strong>Using itertools.permutations</strong>:
<ul class="wp-block-list">
<li>Strength: Incredibly simple and straightforward to use.</li>



<li>Weakness: Can consume a lot of memory with larger input sequences.</li>
</ul>
</li>



<li><strong>Using recursion</strong>:
<ul class="wp-block-list">
<li>Strength: Doesn&#8217;t require any additional modules.</li>



<li>Weakness: Can be less efficient and harder to understand for those not familiar with recursion.</li>
</ul>
</li>



<li><strong>Using Heap&#8217;s Algorithm</strong>:
<ul class="wp-block-list">
<li>Strength: More efficient than naive recursion.</li>



<li>Weakness: Algorithm could be non-intuitive for some users.</li>
</ul>
</li>



<li><strong>Using the sympy library</strong>:
<ul class="wp-block-list">
<li>Strength: Integrates well if already using sympy for other computations.</li>



<li>Weakness: Overhead of using a heavy library for a task achievable with standard libraries.</li>
</ul>
</li>
</ul>



<p>For quick and easy implementation, <code>itertools.permutations</code> is very handy. If learning or teaching recursion and backtracking algorithms, then methods 2 and 3 with recursion and Heap&#8217;s algorithm may be preferable. </p>



<p>When working in a scientific computing environment, <code>sympy</code> could be a natural choice. </p>



<h2 class="wp-block-heading">Python One-Liners Book: Master the Single Line First!</h2>



<p><strong>Python programmers will improve their computer science skills with these useful one-liners.</strong></p>



<div class="wp-block-image"><figure class="aligncenter size-medium is-resized"><a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noopener noreferrer"><img loading="lazy" decoding="async" src="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-1024x944.jpg" alt="Python One-Liners" class="wp-image-10007" width="512" height="472" srcset="https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-300x277.jpg 300w, https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-768x708.jpg 768w" sizes="auto, (max-width: 512px) 100vw, 512px" /></a></figure></div>



<p><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener" title="https://amzn.to/2WAYeJE"><em>Python One-Liners</em> </a>will teach you how to read and write &#8220;one-liners&#8221;: <strong><em>concise statements of useful functionality packed into a single line of code. </em></strong>You&#8217;ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.</p>



<p>The book&#8217;s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. </p>



<p>Detailed explanations of one-liners introduce <strong><em>key computer science concepts </em></strong>and<strong><em> boost your coding and analytical skills</em></strong>. You&#8217;ll learn about advanced Python features such as <em><strong>list comprehension</strong></em>, <strong><em>slicing</em></strong>, <strong><em>lambda functions</em></strong>, <strong><em>regular expressions</em></strong>, <strong><em>map </em></strong>and <strong><em>reduce </em></strong>functions, and <strong><em>slice assignments</em></strong>. </p>



<p>You&#8217;ll also learn how to:</p>



<ul class="wp-block-list"><li>Leverage data structures to <strong>solve real-world problems</strong>, like using Boolean indexing to find cities with above-average pollution</li><li>Use <strong>NumPy basics</strong> such as <em>array</em>, <em>shape</em>, <em>axis</em>, <em>type</em>, <em>broadcasting</em>, <em>advanced indexing</em>, <em>slicing</em>, <em>sorting</em>, <em>searching</em>, <em>aggregating</em>, and <em>statistics</em></li><li>Calculate basic <strong>statistics </strong>of multidimensional data arrays and the K-Means algorithms for unsupervised learning</li><li>Create more <strong>advanced regular expressions</strong> using <em>grouping </em>and <em>named groups</em>, <em>negative lookaheads</em>, <em>escaped characters</em>, <em>whitespaces, character sets</em> (and <em>negative characters sets</em>), and <em>greedy/nongreedy operators</em></li><li>Understand a wide range of <strong>computer science topics</strong>, including <em>anagrams</em>, <em>palindromes</em>, <em>supersets</em>, <em>permutations</em>, <em>factorials</em>, <em>prime numbers</em>, <em>Fibonacci </em>numbers, <em>obfuscation</em>, <em>searching</em>, and <em>algorithmic sorting</em></li></ul>



<p>By the end of the book, you&#8217;ll know how to <strong><em>write Python at its most refined</em></strong>, and create concise, beautiful pieces of &#8220;Python art&#8221; in merely a single line.</p>



<p><strong><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noreferrer noopener" title="https://amzn.to/2WAYeJE"><em>Get your Python One-Liners on Amazon!!</em></a></strong></p>
<p>The post <a href="https://blog.finxter.com/4-best-ways-to-create-a-list-of-permutations-in-python/">4 Best Ways to Create a List of Permutations in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>LLM in a Flash &#8211; Apple&#8217;s Attempt to Inject Intelligence Into the Edge</title>
		<link>https://blog.finxter.com/llm-in-a-flash-apples-attempt-to-inject-intelligence-into-the-edge/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Fri, 22 Dec 2023 13:51:00 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Large Language Model (LLM)]]></category>
		<category><![CDATA[Research]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1653703</guid>

					<description><![CDATA[<p>A new Apple research paper is taking the world by storm: LLM in a flash. ⚡ The ideas is simple: Making large language models (LLMs) work better on computers that don&#8217;t have a lot of memory. 👉 If you&#8217;re a human reader, chances are you&#8217;re reading this on such a computer or smartphone. If you&#8217;re ... <a title="LLM in a Flash &#8211; Apple&#8217;s Attempt to Inject Intelligence Into the Edge" class="read-more" href="https://blog.finxter.com/llm-in-a-flash-apples-attempt-to-inject-intelligence-into-the-edge/" aria-label="Read more about LLM in a Flash &#8211; Apple&#8217;s Attempt to Inject Intelligence Into the Edge">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/llm-in-a-flash-apples-attempt-to-inject-intelligence-into-the-edge/">LLM in a Flash &#8211; Apple&#8217;s Attempt to Inject Intelligence Into the Edge</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A new Apple research paper is taking the world by storm: <em><a href="https://arxiv.org/abs/2312.11514" data-type="link" data-id="https://arxiv.org/abs/2312.11514">LLM in a flash</a>.</em> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/26a1.png" alt="⚡" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p class="has-global-color-8-background-color has-background">The ideas is simple: <strong>Making large language models (LLMs) work better on computers that don&#8217;t have a lot of memory. </strong></p>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <em>If you&#8217;re a human reader, chances are you&#8217;re reading this on such a computer or smartphone. If you&#8217;re an AI model, you&#8217;re probably living in a huge datacenter and are endowed with a lot of memory so don&#8217;t bother reading this. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></em></p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="784" height="784" src="https://blog.finxter.com/wp-content/uploads/2023/12/finxter.com_Young_person_holds_intelligent_Apple_iPhone_smiles__a1122b26-a0f9-4955-8351-cbafeb67034a.webp" alt="" class="wp-image-1653709" srcset="https://blog.finxter.com/wp-content/uploads/2023/12/finxter.com_Young_person_holds_intelligent_Apple_iPhone_smiles__a1122b26-a0f9-4955-8351-cbafeb67034a.webp 784w, https://blog.finxter.com/wp-content/uploads/2023/12/finxter.com_Young_person_holds_intelligent_Apple_iPhone_smiles__a1122b26-a0f9-4955-8351-cbafeb67034a-300x300.webp 300w, https://blog.finxter.com/wp-content/uploads/2023/12/finxter.com_Young_person_holds_intelligent_Apple_iPhone_smiles__a1122b26-a0f9-4955-8351-cbafeb67034a-150x150.webp 150w, https://blog.finxter.com/wp-content/uploads/2023/12/finxter.com_Young_person_holds_intelligent_Apple_iPhone_smiles__a1122b26-a0f9-4955-8351-cbafeb67034a-768x768.webp 768w" sizes="auto, (max-width: 784px) 100vw, 784px" /></figure>
</div>


<p>Normally, LLMs need a lot of computing power and memory to work well, even for inference, i.e., asking the trained model to give you a response.</p>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4ac.png" alt="💬" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Quote</strong>: <em>&#8220;Currently, the standard approach is to load the entire model into DRAM for inference [&#8230;], this severely limits the maximum model size that can be run. For example, a 7 billion parameter model requires over 14GB of memory just to load the<br>parameters in half-precision floating point format, exceeding the capabilities of most edge devices.&#8221;</em></p>



<p>Apple researchers found a way to use less memory by storing the program&#8217;s data on a different kind of memory (<strong>flash memory</strong>, that&#8217;s why the name of the paper) and only moving it to the main memory (<strong>DRAM</strong>) when needed.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="484" height="258" src="https://blog.finxter.com/wp-content/uploads/2023/12/image-117.png" alt="" class="wp-image-1653705" srcset="https://blog.finxter.com/wp-content/uploads/2023/12/image-117.png 484w, https://blog.finxter.com/wp-content/uploads/2023/12/image-117-300x160.png 300w" sizes="auto, (max-width: 484px) 100vw, 484px" /><figcaption class="wp-element-caption"><strong>Bandwidth in memory architecture</strong>: Flash has low bandwidth but high storage capabilities. DRAM has high bandwidth but low storage capabilities. (<a href="https://arxiv.org/pdf/2312.11514.pdf" data-type="link" data-id="https://arxiv.org/pdf/2312.11514.pdf">source</a>)</figcaption></figure>
</div>


<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Info</strong>: Flash memory is a type of <em>non-volatile</em> storage that retains data without power and is commonly used in USB drives and SSDs, whereas DRAM (Dynamic Random Access Memory) is <em>volatile</em> memory used for fast data access in computers, losing its data when the power is off. For example, the songs stored on your MP3 player are on flash memory, while the programs running on your computer use DRAM.</p>



<p><strong>Flash is slow but safe and DRAM is fast but unsafe.</strong> Apple researchers found a way to combine both strengths to get a safe but fast LLM infrastructure.</p>



<p>They did this by figuring out the best way to use flash memory. </p>



<p>They focused on two main things: </p>



<ul class="wp-block-list">
<li>1) using the same data again without having to move it back and forth, and </li>



<li>2) getting data from flash memory in big, uninterrupted pieces which is quicker.</li>
</ul>



<p>They used two special techniques: &#8220;<strong>windowing</strong>&#8220;, which helps reuse data, and &#8220;<strong>row-column bundling</strong>&#8220;, which is about getting data in big chunks that work well with flash memory.</p>



<p>You can see how the active neurons don&#8217;t change a lot from one sliding window to the next:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="515" height="289" src="https://blog.finxter.com/wp-content/uploads/2023/12/image-118.png" alt="" class="wp-image-1653706" srcset="https://blog.finxter.com/wp-content/uploads/2023/12/image-118.png 515w, https://blog.finxter.com/wp-content/uploads/2023/12/image-118-300x168.png 300w" sizes="auto, (max-width: 515px) 100vw, 515px" /><figcaption class="wp-element-caption"><a href="https://arxiv.org/pdf/2312.11514.pdf" data-type="link" data-id="https://arxiv.org/pdf/2312.11514.pdf">source</a></figcaption></figure>
</div>


<p>With these methods, they were able to run big language programs on computers with half the memory normally needed. </p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="468" height="332" src="https://blog.finxter.com/wp-content/uploads/2023/12/image-116.png" alt="" class="wp-image-1653704" srcset="https://blog.finxter.com/wp-content/uploads/2023/12/image-116.png 468w, https://blog.finxter.com/wp-content/uploads/2023/12/image-116-300x213.png 300w" sizes="auto, (max-width: 468px) 100vw, 468px" /><figcaption class="wp-element-caption"><a href="https://arxiv.org/pdf/2312.11514.pdf" data-type="link" data-id="https://arxiv.org/pdf/2312.11514.pdf">source</a></figcaption></figure>
</div>


<p>The programs ran 4-5 times faster on regular CPUs and 20-25 times faster on more powerful GPUs, compared to older methods.</p>



<p>Their approach is smart because it considers how the hardware works and adapts to it, making it possible to use these big programs on devices with less memory.</p>



<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Takeaway</strong>: <a href="https://blog.finxter.com/alien-technology-catching-up-on-llms-prompting-chatgpt-plugins-embeddings-code-interpreter/" data-type="post" data-id="1553158">Alien technology</a> is about to be put in every single device and every imaginable object. The world around us is waking up, starting to sense the environment and react to subtle changes. Intelligence is about to get truly ubiquitous and ambient and we can only guess how much an intelligent environment will disrupt the world we know. For you, this means you need to adapt and do it quickly.</p>



<p><strong>Action Step</strong>: Can you start a small home-based business that puts a Raspberry Pi connecting a <a href="https://blog.finxter.com/5-best-open-source-llms-in-2023-two-minute-guide/" data-type="post" data-id="1651783">small local LLM</a> (no WiFi!) with an everyday object to make it aware of its surrounding? </p>



<p>Brainstorm a few ideas of things you could manufacture and sell at a high profit!</p>



<p class="has-global-color-8-background-color has-background">To be on the right side of change and stay sharp in the age of generative AI, follow Finxter on <a href="https://www.whatsapp.com/channel/0029VaGwhM4BFLgRWzbk5O1A" data-type="link" data-id="https://www.whatsapp.com/channel/0029VaGwhM4BFLgRWzbk5O1A">WhatsApp</a> and <a href="https://blog.finxter.com/subscribe/" data-type="page" data-id="1414">email</a> (free).</p>
<p>The post <a href="https://blog.finxter.com/llm-in-a-flash-apples-attempt-to-inject-intelligence-into-the-edge/">LLM in a Flash &#8211; Apple&#8217;s Attempt to Inject Intelligence Into the Edge</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Swap Function in Python: 5 Most Pythonic Ways to Swap</title>
		<link>https://blog.finxter.com/swap-function-in-python/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Sat, 04 Nov 2023 21:34:05 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Python Built-in Functions]]></category>
		<category><![CDATA[Python One-Liners]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1652684</guid>

					<description><![CDATA[<p>Several methods are available to implement a swap function in Python, including tuple assignment and XOR. Tuple Assignment Swap Method The tuple assignment method creates two tuples with two variables each. The first tuple contains the original variables, while the second one has their exchanged values. Finally, these tuples are &#8220;unpacked&#8221; into individual variables, effectively ... <a title="Swap Function in Python: 5 Most Pythonic Ways to Swap" class="read-more" href="https://blog.finxter.com/swap-function-in-python/" aria-label="Read more about Swap Function in Python: 5 Most Pythonic Ways to Swap">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/swap-function-in-python/">Swap Function in Python: 5 Most Pythonic Ways to Swap</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Several methods are available to implement a <a href="https://blog.finxter.com/what-is-the-swap-function-in-python/">swap function</a> in Python, including <strong>tuple assignment</strong> and <strong>XOR</strong>. </p>



<h2 class="wp-block-heading">Tuple Assignment Swap Method</h2>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="How to Swap Two Variables in One Line Python?" width="937" height="527" src="https://www.youtube.com/embed/RTRDwqV5sBA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="has-global-color-8-background-color has-background">The <strong>tuple assignment method</strong> creates two tuples with two variables each. The first tuple contains the original variables, while the second one has their exchanged values. Finally, these tuples are &#8220;<a href="https://blog.finxter.com/python-unpacking/">unpacked</a>&#8221; into individual variables, effectively completing the swap process. This technique allows you to swap values in a single statement without needing a temporary variable. </p>



<p>For 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="">a, b = b, a
</pre>



<p>When you execute <code>a, b = b, a</code> in Python, the following happens:</p>



<ol class="wp-block-list">
<li><strong>Tuple Packing</strong>: The right-hand side <code>b, a</code> creates a tuple with the current values of <code>b</code> and <code>a</code>. No actual tuple object is created in memory; it&#8217;s just a conceptual packing.</li>



<li><strong>Value Assignment</strong>: Python then unpacks the tuple into the variables on the left-hand side in the order they are listed. The first element of the tuple (the original <code>b</code>) is assigned to <code>a</code>, and the second element (the original <code>a</code>) is assigned to <code>b</code>.</li>



<li><strong>Simultaneous Variable Update</strong>: Both assignments happen virtually simultaneously. There is no intermediate state where one variable has been changed but the other has not, which is why the swap can occur without an additional temporary variable.</li>
</ol>



<p>Python handles this operation elegantly and atomically, ensuring that the variables are swapped without any need for a temporary storage location.</p>



<h2 class="wp-block-heading">XOR Method for Swapping</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="701" src="https://blog.finxter.com/wp-content/uploads/2023/11/image-4-1024x701.jpeg" alt="" class="wp-image-1652686" style="aspect-ratio:1.4607703281027105;width:729px;height:auto" srcset="https://blog.finxter.com/wp-content/uploads/2023/11/image-4-1024x701.jpeg 1024w, https://blog.finxter.com/wp-content/uploads/2023/11/image-4-300x205.jpeg 300w, https://blog.finxter.com/wp-content/uploads/2023/11/image-4-768x525.jpeg 768w, https://blog.finxter.com/wp-content/uploads/2023/11/image-4.jpeg 1216w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p class="has-global-color-8-background-color has-background">The <strong>XOR method</strong> can be employed as another way to implement the swap function. This method uses bitwise XOR operations to swap the values of two variables. Although slightly more complex, the XOR method can be more efficient in certain scenarios. To perform a swap using the XOR method, you can use the following 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="">a = a ^ b
b = a ^ b
a = a ^ b
</pre>



<p>This method works, for example, when using two integers:</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="">a = 21
b = 42

a = a ^ b
b = a ^ b
a = a ^ b

print(a)
# 42

print(b)
# 21
</pre>



<p>This code snippet uses the <a href="https://blog.finxter.com/python-bitwise-xor-operator/">XOR bitwise operator (<code>^</code>)</a> to swap the values of two variables, <code>a</code> and <code>b</code>, without using a temporary variable. </p>



<p>To recap the XOR operator, feel free to watch my explainer video:</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python Bitwise XOR ^ Operator" width="937" height="527" src="https://www.youtube.com/embed/2bU03PDXQ00?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Here&#8217;s what happens in detail:</p>



<ol class="wp-block-list">
<li><code>a = a ^ b</code>: The XOR operation is performed between <code>a</code> (21) and <code>b</code> (42). The result of this operation is stored back in <code>a</code>. The property of XOR is that two identical bits result in 0 and two different bits result in 1. This effectively encodes the values of <code>a</code> and <code>b</code> into <code>a</code>.</li>



<li><code>b = a ^ b</code>: Now, the new value of <code>a</code> is XORed with <code>b</code>. Since the current <code>a</code> contains the encoded original values of <code>a</code> and <code>b</code>, this operation decodes the original value of <code>a</code> and assigns it to <code>b</code>.</li>



<li><code>a = a ^ b</code>: Finally, the new <code>a</code> (which is the encoded original values) is XORed with the new <code>b</code> (which is now the original value of <code>a</code>). This decodes back to the original value of <code>b</code> and assigns it to <code>a</code>.</li>
</ol>



<p>The XOR swap algorithm takes advantage of the fact that XORing a number with itself results in zero, and XORing a number with zero results in the original number. This allows the original values of <code>a</code> and <code>b</code> to be swapped without the need for a temporary storage variable.</p>



<p>After this sequence of operations, <code>a</code> becomes 42 and <code>b</code> becomes 21, which is confirmed by the print statements.</p>



<h2 class="wp-block-heading">String Swapping with Unpacking</h2>



<p></p>



<p class="has-global-color-8-background-color has-background">The <strong>unpacking approach</strong> is the underlying principle behind this simple swap operation in Python. It allows you to easily rearrange or exchange the values of several variables simultaneously (e.g., <code>a, b, c = c, a, b</code>). This makes it a powerful and versatile method for managing data in your code.</p>



<p>This simple yet effective method enables you to swap values without the need for any additional temporary variables or complex procedures. It works for any data type, including numbers and strings. For instance, let&#8217;s consider the following 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=""># Swapping numbers
x = 5
y = 10
x, y = y, x
print(x, y)  # Output: 10 5

# Swapping strings
str1 = "Hello"
str2 = "World"
str1, str2 = str2, str1
print(str1, str2)  # Output: World Hello
</pre>



<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9d1-200d-1f4bb.png" alt="🧑‍💻" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/python-unpacking/">Python Unpacking [Ultimate Guide]</a></p>



<p></p>



<h2 class="wp-block-heading">List Swapping</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="701" src="https://blog.finxter.com/wp-content/uploads/2023/11/image-3-1024x701.jpeg" alt="" class="wp-image-1652685" srcset="https://blog.finxter.com/wp-content/uploads/2023/11/image-3-1024x701.jpeg 1024w, https://blog.finxter.com/wp-content/uploads/2023/11/image-3-300x205.jpeg 300w, https://blog.finxter.com/wp-content/uploads/2023/11/image-3-768x525.jpeg 768w, https://blog.finxter.com/wp-content/uploads/2023/11/image-3.jpeg 1216w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>Another scenario where swapping is required involves lists. Suppose you&#8217;re working with a list in Python and need to exchange the positions of two elements. You can use the power of tuple unpacking and list indexing to achieve this quickly:</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="">my_list = [23, 65, 19, 90]
pos1, pos2 = 0, 2
my_list[pos1], my_list[pos2] = my_list[pos2], my_list[pos1]
</pre>



<p>The given code snippet swaps the elements at positions <code>pos1</code> and <code>pos2</code> in the list <code>my_list</code>.</p>



<p>Here&#8217;s the process:</p>



<ul class="wp-block-list">
<li><code>my_list</code> starts as <code>[23, 65, 19, 90]</code>.</li>



<li><code>pos1</code> is set to <code>0</code>, and <code>pos2</code> is set to <code>2</code>, meaning we&#8217;ll be swapping the elements at the first and third positions in the list (indexing starts at 0 in Python).</li>



<li>The swap is done in a Pythonic way, similar to the variable swap discussed earlier: <code>my_list[pos1], my_list[pos2] = my_list[pos2], my_list[pos1]</code>.</li>



<li>This line creates a tuple from the elements at the specified positions and then unpacks them back into the list at the swapped positions.</li>
</ul>



<p>After this line of code executes, the list <code>my_list</code> is modified to <code>[19, 65, 23, 90]</code> because the elements at indices <code>0</code> and <code>2</code> have been swapped.</p>



<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="">[19, 65, 23, 90]</pre>



<p></p>



<h2 class="wp-block-heading">Tuple Swapping</h2>



<p class="has-global-color-8-background-color has-background">Unlike lists, <strong>tuples</strong> are <a href="https://blog.finxter.com/mutable-vs-immutable-objects-in-python/">immutable</a>, which means their values cannot be modified once they are created. Due to their immutability, you cannot swap elements directly within a tuple. Instead, you can create a new tuple with the swapped elements using a combination of indexing and tuple concatenation. </p>



<p>For 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="">original_tuple = (1, 2, 3, 4, 5)
index1, index2 = 1, 3

# Create a new tuple with swapped elements
swapped_tuple = original_tuple[:index1] + (original_tuple[index2],) + original_tuple[index1+1:index2] + (original_tuple[index1],) + original_tuple[index2+1:]

print(swapped_tuple)  # Output: (1, 4, 3, 2, 5)
</pre>



<p>The code snippet demonstrates how to swap elements in a tuple, which is an immutable sequence in Python. Since tuples cannot be modified after creation, you must create a new tuple to represent the swapped state. Here&#8217;s how the swapping process works in the given code:</p>



<ul class="wp-block-list">
<li><code>original_tuple</code> is defined as <code>(1, 2, 3, 4, 5)</code>.</li>



<li><code>index1</code> and <code>index2</code> are set to <code>1</code> and <code>3</code>, respectively, indicating the positions of elements in the tuple that need to be swapped (keeping in mind that Python uses 0-based indexing).</li>
</ul>



<p>The <code>swapped_tuple</code> is constructed as follows:</p>



<ol class="wp-block-list">
<li><code>original_tuple[:index1]</code>: Selects all elements from the start of the tuple up to but not including the element at <code>index1</code>. In this case, it&#8217;s <code>(1,)</code>.</li>



<li><code>(original_tuple[index2],)</code>: Creates a new tuple containing just the element at <code>index2</code>. The comma is necessary to indicate it&#8217;s a tuple with one element: <code>(4,)</code>.</li>



<li><code>original_tuple[index1+1:index2]</code>: Selects the elements between <code>index1</code> and <code>index2</code>, not including the element at <code>index2</code>: <code>(3,)</code>.</li>



<li><code>(original_tuple[index1],)</code>: Similar to step 2, this creates a tuple with the element at <code>index1</code>: <code>(2,)</code>.</li>



<li><code>original_tuple[index2+1:]</code>: Selects all the elements after <code>index2</code> to the end of the original tuple: <code>(5,)</code>.</li>
</ol>



<p>These parts are concatenated using the <code>+</code> operator to form <code>swapped_tuple</code>. When you print <code>swapped_tuple</code>, the output is <code>(1, 4, 3, 2, 5)</code>, showing that the elements at the 1st index (<code>2</code>) and the 3rd index (<code>4</code>) have been swapped.</p>



<h2 class="wp-block-heading">Tuple Swapping Using List Swapping</h2>



<p>Another approach to swapping elements in tuples is converting the tuple to a list, performing the swap on the list, and then converting the list back to 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="">original_tuple = (1, 2, 3, 4, 5)
index1, index2 = 1, 3

# Convert tuple to list
temp_list = list(original_tuple)

# Swap elements in the list
temp_list[index1], temp_list[index2] = temp_list[index2], temp_list[index1]

# Convert list back to tuple
swapped_tuple = tuple(temp_list)

print(swapped_tuple)  # Output: (1, 4, 3, 2, 5)
</pre>



<h2 class="wp-block-heading">Generalized swap() Function</h2>



<p>In certain situations, you may need to swap variable values of different types using the <code>swap()</code> function. To accomplish this, you can harness the flexibility of Python&#8217;s built-in functions by creating a custom swap function:</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 swap(x, y):
    return y, x

a = 'Hello'
b = 42
a, b = swap(a, b)
</pre>



<p>This custom function takes two variables as input and returns their swapped values. By employing such a function, you can swap variables of any type seamlessly.</p>



<p></p>



<h2 class="wp-block-heading">A Few Words on Those Temporary Variables</h2>



<figure class="wp-block-image"><img decoding="async" src="https://koala.sh/api/image/v2-1dcko-ybfej.jpg?width=1216&amp;height=832&amp;dream" alt=""/></figure>



<p>In many programming languages, including Python, you may need to swap the values of two variables. One common way to achieve this is by using a temporary variable. A temporary variable serves as a placeholder to store the original value of one of the variables before reassigning its value.</p>



<p>For instance, consider the following Python code which swaps the values of <code>a</code> and <code>b</code> using a temporary variable:</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="">a = 5
b = 10

temp = a
a = b
b = temp

print("a =", a)
print("b =", b)
</pre>



<p>Here&#8217;s a breakdown of the code:</p>



<ol class="wp-block-list">
<li><code>temp = a</code>: The value of <code>a</code> is assigned to the temporary variable <code>temp</code>.</li>



<li><code>a = b</code>: The value of <code>b</code> is assigned to <code>a</code>. Now both variables <code>a</code> and <code>b</code> have the same value (10).</li>



<li><code>b = temp</code>: The original value of <code>a</code> that was stored in the temporary variable <code>temp</code> is now assigned back to <code>b</code>.</li>
</ol>



<p>After executing this code, the values of <code>a</code> and <code>b</code> will be swapped, with <code>a</code> holding the value 10 and <code>b</code> holding the value 5.</p>



<p class="has-global-color-8-background-color has-background">Using a temporary variable is a straightforward and intuitive approach to swap the values of two variables in Python. However, Python also offers other methods for swapping values without introducing a temporary variable, such as tuple unpacking (<code>a, b = b, a</code>). </p>



<p></p>



<h2 class="wp-block-heading">Swapping With Array or List</h2>



<p>You can also use an array to swap elements in a Python list. To do this, you need to pop the elements at both positions <code>pos1</code> and <code>pos2</code>, storing them in temporary variables. Then, <a href="https://blog.finxter.com/python-list-insert-method/">insert</a> these elements back into the list at their opposite positions.</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 swap_positions_with_array(list, pos1, pos2):
    first_element = list.pop(pos1)
    second_element = list.pop(pos2 - 1)
    list.insert(pos1, second_element)
    list.insert(pos2, first_element)
    return list
</pre>



<p>The <code>swap_positions_with_array</code> function is designed to swap two elements at specific positions within a list, without using the typical tuple unpacking method. It does this by directly manipulating the list using <code>pop</code> and <code>insert</code> methods. Here’s how it works:</p>



<ol class="wp-block-list">
<li><code>first_element = list.pop(pos1)</code>: Removes the element at <code>pos1</code> from the list and stores it in <code>first_element</code>.</li>



<li><code>second_element = list.pop(pos2 - 1)</code>: After the first pop, all elements shift one position to the left. So, the element at <code>pos2</code> is now at <code>pos2 - 1</code>. This element is removed and stored in <code>second_element</code>.</li>



<li><code>list.insert(pos1, second_element)</code>: Inserts <code>second_element</code> at <code>pos1</code>. This shifts elements to the right from this position onwards.</li>



<li><code>list.insert(pos2, first_element)</code>: Inserts <code>first_element</code> at the original <code>pos2</code>. Since we had removed one element before this point, the insert will place <code>first_element</code> correctly at <code>pos2</code>.</li>
</ol>



<p>The function then returns the modified list with the elements swapped.</p>



<p><strong>Example</strong>:</p>



<p>Let&#8217;s say we have a list <code>[10, 20, 30, 40, 50]</code> and we want to swap the elements at positions <code>1</code> (the element <code>20</code>) and <code>3</code> (the element <code>40</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="">my_list = [10, 20, 30, 40, 50]
swapped_list = swap_positions_with_array(my_list, 1, 3)
print(swapped_list)  # Output will be [10, 40, 30, 20, 50]</pre>



<p>In the output, the elements <code>20</code> and <code>40</code> have been swapped.</p>



<p></p>



<h2 class="wp-block-heading">Memory and Arithmetic Operations in Swap Function</h2>



<figure class="wp-block-image"><img decoding="async" src="https://koala.sh/api/image/v2-1dcok-2nkdb.jpg?width=1216&amp;height=832&amp;dream" alt=""/></figure>



<p class="has-global-color-8-background-color has-background">Another approach is using <strong>arithmetic operations</strong> to swap values without a temporary variable, mainly when working with numeric variables. This method involves various mathematical operations like addition, subtraction, or bitwise operators. </p>



<p>Here&#8217;s an example using addition and subtraction:</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="">x = 5
y = 10
x = x + y
y = x - y
x = x - y
</pre>



<p>This code snippet is a method of swapping the values of two variables without using a temporary third variable. Here&#8217;s a step-by-step explanation of how it works:</p>



<p>Initially:</p>



<ul class="wp-block-list">
<li><code>x</code> is 5</li>



<li><code>y</code> is 10</li>
</ul>



<ol class="wp-block-list">
<li><code>x = x + y</code> adds the value of <code>y</code> to <code>x</code>:
<ul class="wp-block-list">
<li>Now <code>x</code> is 15 (the sum of the initial values of <code>x</code> and <code>y</code>).</li>



<li><code>y</code> remains 10.</li>
</ul>
</li>



<li><code>y = x - y</code> subtracts the new value of <code>x</code> by the current value of <code>y</code> to find the original value of <code>x</code>:
<ul class="wp-block-list">
<li>Now <code>y</code> is 5 (which was the initial value of <code>x</code>).</li>



<li><code>x</code> remains 15.</li>
</ul>
</li>



<li><code>x = x - y</code> subtracts the new value of <code>y</code> from the current value of <code>x</code> to find the original value of <code>y</code>:
<ul class="wp-block-list">
<li>Now <code>x</code> is 10 (which was the initial value of <code>y</code>).</li>



<li><code>y</code> remains 5.</li>
</ul>
</li>
</ol>



<p>After these operations, <code>x</code> and <code>y</code> have effectively swapped values:</p>



<ul class="wp-block-list">
<li><code>x</code> is now 10</li>



<li><code>y</code> is now 5</li>
</ul>



<p>This is a classic programming trick used to save memory by avoiding the need for an additional variable to hold a value temporarily during the swap.</p>



<h2 class="wp-block-heading">Sorting and Swap Function</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="701" src="https://blog.finxter.com/wp-content/uploads/2023/11/image-6-1024x701.jpeg" alt="" class="wp-image-1652689" srcset="https://blog.finxter.com/wp-content/uploads/2023/11/image-6-1024x701.jpeg 1024w, https://blog.finxter.com/wp-content/uploads/2023/11/image-6-300x205.jpeg 300w, https://blog.finxter.com/wp-content/uploads/2023/11/image-6-768x525.jpeg 768w, https://blog.finxter.com/wp-content/uploads/2023/11/image-6.jpeg 1216w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>When working with lists in Python, you may need to sort or rearrange data. The <strong>sort</strong> function is a helpful tool for ordering elements in a list. In addition to the built-in sorting methods, you can also implement custom sorting methods by utilizing the concept of the <strong>swap function</strong>.</p>



<p>A swap function is a simple method that exchanges the positions of two elements in a list. It can be particularly useful in custom sorting algorithms, such as bubble sort or insertion sort. Here&#8217;s how you can create a basic swap function in Python:</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 swap(arr, i, j):
    arr[i], arr[j] = arr[j], arr[i]
</pre>



<p>In this function, <code>arr</code> is the input list, and <code>i</code> and <code>j</code> are the indices of the elements you want to swap. The function directly manipulates the original list and does not return a new list.</p>



<p>Now, let&#8217;s see how you can use the swap function in a simple sorting algorithm like <a href="https://blog.finxter.com/daily-python-puzzle-bubble-sort/">bubble sort</a>:</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 bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                swap(arr, j, j+1)
</pre>



<p>In this implementation, the <code>bubble_sort</code> function iterates through the list and compares adjacent elements. If the current element is <a href="https://blog.finxter.com/python-greater-than/">greater than</a> the next, it calls the <code>swap</code> function to swap their positions. This process continues until the list is sorted.</p>



<p></p>



<h2 class="wp-block-heading">Frequently Asked Questions</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="701" src="https://blog.finxter.com/wp-content/uploads/2023/11/image-7-1024x701.jpeg" alt="" class="wp-image-1652690" srcset="https://blog.finxter.com/wp-content/uploads/2023/11/image-7-1024x701.jpeg 1024w, https://blog.finxter.com/wp-content/uploads/2023/11/image-7-300x205.jpeg 300w, https://blog.finxter.com/wp-content/uploads/2023/11/image-7-768x525.jpeg 768w, https://blog.finxter.com/wp-content/uploads/2023/11/image-7.jpeg 1216w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">How can you swap two elements in a Python array?</h3>



<p>To swap two elements in a Python array (also known as a list), you can use a temporary variable to store the value of one element while assigning the value of the other element. For 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="">list_name = [1, 2, 3, 4]
index1 = 1
index2 = 3
temp = list_name[index1]
list_name[index1] = list_name[index2]
list_name[index2] = temp
</pre>



<p>This will swap the elements at index positions 1 and 3 in the list <code>list_name</code>.</p>



<h3 class="wp-block-heading">What is the process for swapping values of two variables in Python?</h3>



<p>In Python, you can swap values of two variables without using a temporary variable. The method commonly used is called <a href="https://blogboard.io/blog/knowledge/python-swap/">tuple unpacking</a>. Here&#8217;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="">x = 5
y = 10
x, y = y, x
</pre>



<p>Now, <code>x</code> will have the value <code>10</code>, and <code>y</code> will have the value <code>5</code>.</p>



<h3 class="wp-block-heading">How can you reverse an array or string in Python?</h3>



<p>To reverse an array (list) or string in Python, you can use slicing. For 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="">my_list = [1, 2, 3, 4]
my_string = "Hello"
reversed_list = my_list[::-1]
reversed_string = my_string[::-1]
</pre>



<p>After executing this code, <code>reversed_list</code> will contain <code>[4, 3, 2, 1]</code> and <code>reversed_string</code> will contain <code>"olleH"</code>.</p>



<h3 class="wp-block-heading">What is the role of a temporary variable in variable swapping?</h3>



<p>A temporary variable is used to temporarily store the value of a variable when you need to swap the values of two variables. This method creates a temporary &#8220;place holder&#8221; to prevent data loss during the swapping process. In Python, however, using a temporary variable is unnecessary thanks to tuple unpacking, as mentioned earlier.</p>



<h3 class="wp-block-heading">How is the replace function used in Python?</h3>



<p>The <code><a href="https://blog.finxter.com/python-string-replace-2/">replace()</a></code> function in Python is a string method that allows you to replace occurrences of a given substring with a new substring. Here&#8217;s an example of how to use the <code>replace()</code> function:</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="">original_string = "I love apples and apples are tasty."
new_string = original_string.replace("apples", "oranges")
</pre>



<p>In this example, <code>new_string</code> will contain the text: <code>"I love oranges and oranges are tasty."</code></p>



<h3 class="wp-block-heading">Can bubble sort or other sorting methods be used to swap elements?</h3>



<p>Yes, bubble sort and other sorting algorithms can be used to swap elements in a list during the sorting process. In fact, swapping elements is a crucial part of many sorting algorithms. For example, during bubble sort, adjacent elements are compared and swapped if they are in the wrong order, ultimately sorting the list through a series of swaps. Similarly, other sorting algorithms like selection sort and insertion sort also involve element swapping as a primary operation.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://blog.finxter.com/making-money-with-python/"><img loading="lazy" decoding="async" width="1024" height="572" src="https://blog.finxter.com/wp-content/uploads/2023/11/image-129-1024x572.png" alt="" class="wp-image-1652691" srcset="https://blog.finxter.com/wp-content/uploads/2023/11/image-129-1024x572.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/11/image-129-300x168.png 300w, https://blog.finxter.com/wp-content/uploads/2023/11/image-129-768x429.png 768w, https://blog.finxter.com/wp-content/uploads/2023/11/image-129.png 1381w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>
</div>


<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9d1-200d-1f4bb.png" alt="🧑‍💻" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/making-money-with-python/">55 Best Ideas to Make Money with Python</a></p>
<p>The post <a href="https://blog.finxter.com/swap-function-in-python/">Swap Function in Python: 5 Most Pythonic Ways to Swap</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Transformer vs LSTM: A Helpful Illustrated Guide</title>
		<link>https://blog.finxter.com/transformer-vs-lstm/</link>
		
		<dc:creator><![CDATA[Emily Rosemary Collins]]></dc:creator>
		<pubDate>Tue, 04 Jul 2023 20:26:56 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Large Language Model (LLM)]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1478123</guid>

					<description><![CDATA[<p>In the realm of natural language processing and machine learning, two common and highly effective models for handling sequential data are Transformers and Long Short-Term Memory (LSTM) networks. While both models have proven successful in various applications, they differ in terms of architectural structure and how they process and handle data. LSTM networks, a type ... <a title="Transformer vs LSTM: A Helpful Illustrated Guide" class="read-more" href="https://blog.finxter.com/transformer-vs-lstm/" aria-label="Read more about Transformer vs LSTM: A Helpful Illustrated Guide">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/transformer-vs-lstm/">Transformer vs LSTM: A Helpful Illustrated Guide</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In the realm of natural language processing and machine learning, two common and highly effective models for handling sequential data are <strong>Transformers</strong> and <strong>Long Short-Term Memory (LSTM)</strong> networks. While both models have proven successful in various applications, they differ in terms of architectural structure and how they process and handle data.</p>



<p>LSTM networks, a type of recurrent neural network (RNN), were specifically designed to address the vanishing gradient problem found in standard RNNs. They have the ability to learn and retain long-range dependencies and are often used for sequence-to-sequence tasks, such as language translation or text generation. On the other hand, Transformers have gained popularity in recent years due to their parallelization capabilities and the introduction of the attention mechanism, which allows them to effectively process large, complex sequences without getting bogged down in sequential data processing.</p>



<h2 class="wp-block-heading">Transformer and LSTM Overview</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="817" height="817" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-39.png" alt="" class="wp-image-1478195" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-39.png 817w, https://blog.finxter.com/wp-content/uploads/2023/07/image-39-300x300.png 300w, https://blog.finxter.com/wp-content/uploads/2023/07/image-39-150x150.png 150w, https://blog.finxter.com/wp-content/uploads/2023/07/image-39-768x768.png 768w" sizes="auto, (max-width: 817px) 100vw, 817px" /></figure>
</div>


<p class="has-global-color-8-background-color has-background">Transformers outpace RNN models due to simultaneous input processing and are easier to train than LSTMs due to fewer parameters. Currently, they are the leading technology for seq2seq models.</p>



<p>Transformers and LSTMs are both popular techniques used in the field of natural language processing (NLP) and sequence-to-sequence modeling tasks. Let&#8217;s dive into the key differences and similarities between these two methods. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f916.png" alt="🤖" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>Transformers make use of the <a href="https://deepai.org/machine-learning-glossary-and-terms/transformer-neural-network" target="_blank" rel="noreferrer noopener">attention mechanism</a> that enables them to process and capture crucial aspects of the input data. They do this without relying on recurrent neural networks (RNNs) like LSTMs or gated recurrent units (GRUs). This allows for parallel processing, resulting in faster training times compared to sequential approaches in RNNs.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="523" height="739" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-33.png" alt="" class="wp-image-1478151" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-33.png 523w, https://blog.finxter.com/wp-content/uploads/2023/07/image-33-212x300.png 212w" sizes="auto, (max-width: 523px) 100vw, 523px" /><figcaption class="wp-element-caption"><a href="https://arxiv.org/pdf/1706.03762.pdf" data-type="URL" data-id="https://arxiv.org/pdf/1706.03762.pdf" target="_blank" rel="noreferrer noopener">Image credits</a></figcaption></figure>
</div>


<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/the-evolution-of-large-language-models-llms-insights-from-gpt-4-and-beyond/" data-type="URL" data-id="https://blog.finxter.com/the-evolution-of-large-language-models-llms-insights-from-gpt-4-and-beyond/" target="_blank" rel="noreferrer noopener">The Evolution of Large Language Models (LLMs): Insights from GPT-4 and Beyond</a></p>



<p>On the other hand, LSTMs (Long Short-Term Memory) are a type of RNN specifically designed to overcome the limitations of standard RNNs in handling long-term dependencies. They achieve this through a unique cell structure that includes input, output, and forget gates, controlling the flow of information across time steps.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="810" height="304" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-31.png" alt="" class="wp-image-1478148" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-31.png 810w, https://blog.finxter.com/wp-content/uploads/2023/07/image-31-300x113.png 300w, https://blog.finxter.com/wp-content/uploads/2023/07/image-31-768x288.png 768w" sizes="auto, (max-width: 810px) 100vw, 810px" /><figcaption class="wp-element-caption"><a href="http://colah.github.io/posts/2015-08-Understanding-LSTMs/" data-type="URL" data-id="http://colah.github.io/posts/2015-08-Understanding-LSTMs/" target="_blank" rel="noreferrer noopener">Image source</a></figcaption></figure>
</div>


<p>One key advantage of Transformers over LSTMs is their more effective handling of long-range dependencies, due to the self-attention mechanism. This allows them to weigh the importance of various positions in the input sequence, whereas LSTMs might struggle with retaining information from distant positions in longer sequences.</p>



<p>The architecture of Transformers typically consists of stacked encoder and decoder layers, with self-attention and feed-forward neural network (FFN) layers in each. The absence of RNN cells, as seen in <a href="https://bitshots.github.io/Blogs/rnn-vs-lstm-vs-transformer/" target="_blank" rel="noreferrer noopener">LSTMs</a>, contributes to their parallel processing capabilities.</p>



<p>Both Transformers and LSTMs have shown excellent performance in tasks like machine translation, speech recognition, text classification, and more.</p>



<h2 class="wp-block-heading">Attention Mechanism</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="817" height="817" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-40.png" alt="" class="wp-image-1478197" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-40.png 817w, https://blog.finxter.com/wp-content/uploads/2023/07/image-40-300x300.png 300w, https://blog.finxter.com/wp-content/uploads/2023/07/image-40-150x150.png 150w, https://blog.finxter.com/wp-content/uploads/2023/07/image-40-768x768.png 768w" sizes="auto, (max-width: 817px) 100vw, 817px" /></figure>
</div>


<p>The Attention Mechanism is an important innovation in neural networks that allows models to selectively focus on certain aspects of the input data, rather than processing it all at once. This has proven especially useful in language translation and sequence-to-sequence tasks. The Attention Mechanism has paved the way for more advanced network architectures, such as Transformers, and improved upon LSTM models.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="872" height="812" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-34.png" alt="" class="wp-image-1478153" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-34.png 872w, https://blog.finxter.com/wp-content/uploads/2023/07/image-34-300x279.png 300w, https://blog.finxter.com/wp-content/uploads/2023/07/image-34-768x715.png 768w" sizes="auto, (max-width: 872px) 100vw, 872px" /><figcaption class="wp-element-caption">Screenshot from the <a href="https://arxiv.org/pdf/1706.03762.pdf" data-type="URL" data-id="https://arxiv.org/pdf/1706.03762.pdf" target="_blank" rel="noreferrer noopener">&#8220;Attention is all you need&#8221;</a> paper</figcaption></figure>
</div>


<h3 class="wp-block-heading">Self-Attention</h3>



<p>Self-Attention is a specific type of attention mechanism where a model learns to selectively focus on certain parts of the input sequence to generate more relevant output. It computes a weighted sum of input values, where the weights are obtained by comparing each input to the rest of the inputs in the sequence. This allows the model to implicitly learn the relationships and dependencies between the elements in the sequence <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9e0.png" alt="🧠" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<p>The main components of Self-Attention are queries, keys, and values. The queries are used to compare the input elements, while the keys and values represent the relationship between the elements. The softmax function is applied to the computed attention weights to form a probability distribution, emphasizing the most relevant elements in the sequence.</p>



<h3 class="wp-block-heading">Multi-Head Attention</h3>



<p>In the Transformer model, Multi-Head Attention is utilized to simultaneously focus on different subsets of the input data, allowing the model to learn multiple contextually rich representations of the data in parallel. Instead of using a single attention mechanism, the Multi-Head Attention mechanism consists of several attention heads, each with its own queries, keys, and values.</p>



<p>This design enables the Transformer to capture various aspects of the input sequence, making it more efficient and powerful at handling complex tasks. Each head processes the input independently and then combines the resulting representations through concatenation and a linear transformation.</p>



<h3 class="wp-block-heading">Encoder-Decoder Attention</h3>



<p>Encoder-Decoder Attention is another important aspect of attention mechanism, primarily used in sequence-to-sequence tasks like machine translation. In this setup, the encoder processes the input sequence and generates a context vector, while the decoder generates the output sequence based on this context vector.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="665" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-32-1024x665.png" alt="" class="wp-image-1478150" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-32-1024x665.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/07/image-32-300x195.png 300w, https://blog.finxter.com/wp-content/uploads/2023/07/image-32-768x499.png 768w, https://blog.finxter.com/wp-content/uploads/2023/07/image-32.png 1156w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption"><a href="http://jalammar.github.io/illustrated-transformer/" data-type="URL" data-id="http://jalammar.github.io/illustrated-transformer/" target="_blank" rel="noreferrer noopener">Image source</a></figcaption></figure>
</div>


<p>The Encoder-Decoder Attention mechanism allows the decoder to attend to different parts of the encoded input sequence, promoting greater understanding of the input relationships and generating more accurate output sequences. The encoder&#8217;s output serves as the keys and values, whereas the decoder&#8217;s hidden states act as queries. This setup effectively enables the decoder to align itself to different parts of the input sequence when generating the output, hence leading to improved translation and sequence generation <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f680.png" alt="🚀" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<h2 class="wp-block-heading">Transformers</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="817" height="817" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-41.png" alt="" class="wp-image-1478198" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-41.png 817w, https://blog.finxter.com/wp-content/uploads/2023/07/image-41-300x300.png 300w, https://blog.finxter.com/wp-content/uploads/2023/07/image-41-150x150.png 150w, https://blog.finxter.com/wp-content/uploads/2023/07/image-41-768x768.png 768w" sizes="auto, (max-width: 817px) 100vw, 817px" /></figure>
</div>


<p>Transformers are a type of deep learning architecture that have proven to be very effective, especially in natural language processing tasks. They were introduced in a <a href="https://arxiv.org/abs/1706.03762" target="_blank" rel="noreferrer noopener">paper</a> by Vaswani et al. in 2017. </p>



<p>The transformer model is particularly well-suited for handling long-range dependencies in text and allows for efficient parallel computation. In this section, we will discuss some key aspects of transformers, including encoding and decoding, positional encoding, residual connections, and parallelization.</p>



<h3 class="wp-block-heading">Encoding and Decoding</h3>



<p>The transformer model consists of an encoder and a decoder. Both the encoder and decoder are composed of multiple layers, with each layer containing two main components: multi-head self-attention and position-wise feed-forward networks.</p>



<ul class="wp-block-list">
<li><strong>Encoder</strong>: The encoder takes the input sequence and processes it to generate a continuous representation. This continuous representation preserves the contextual information of the input and can be effectively used by the decoder for generating the target sequence.</li>



<li><strong>Decoder</strong>: The decoder takes this continuous representation from the encoder and generates the target sequence. It also has a multi-head self-attention mechanism, but in addition, it has an encoder-decoder attention mechanism that helps it to focus on different parts of the input sequence.</li>
</ul>



<h3 class="wp-block-heading">Positional Encoding</h3>



<p>Since transformers do not have any recurrent or convolutional structure, they need a way to capture the order of the input sequence. This is where positional encoding comes into play. Positional encoding is added to the input embeddings to provide the model with positional information. This is typically accomplished by adding sine and cosine functions of different frequencies to the input embeddings. These functions help the model to learn and use the positions of the input tokens effectively.</p>



<h3 class="wp-block-heading">Residual Connections</h3>



<p>Residual connections are a vital part of the transformer architecture. They allow the model to preserve information from earlier layers and help in mitigating the vanishing gradient problem. In transformers, each sub-layer (multi-head self-attention and position-wise feed-forward networks) has a residual connection followed by a layer normalization step. This means that the output of each sub-layer is added to its input, and this sum is then normalized before being fed to the next sub-layer.</p>



<h3 class="wp-block-heading">Parallelization</h3>



<p>One of the key advantages of the transformer model over RNNs and LSTMs is its ability to process the input sequence in parallel, rather than sequentially. This is because transformers use self-attention mechanisms that can process multiple words simultaneously instead of relying on recurrent connections that process the input in a sequential manner. This parallel computation capability allows transformers to be highly efficient and scalable, making them ideal for handling large-scale natural language processing tasks. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60a.png" alt="😊" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">LSTM</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="817" height="817" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-42.png" alt="" class="wp-image-1478199" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-42.png 817w, https://blog.finxter.com/wp-content/uploads/2023/07/image-42-300x300.png 300w, https://blog.finxter.com/wp-content/uploads/2023/07/image-42-150x150.png 150w, https://blog.finxter.com/wp-content/uploads/2023/07/image-42-768x768.png 768w" sizes="auto, (max-width: 817px) 100vw, 817px" /></figure>
</div>


<h3 class="wp-block-heading">Gates and States</h3>



<p>Long Short-Term Memory (LSTM) is a type of recurrent neural network (RNN) designed to handle sequence data and address the vanishing gradient problem. It consists of a series of gates and hidden states that help the model remember long-term dependencies in the data. There are three types of gates in an LSTM: input gate, forget gate, and output gate.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="747" height="504" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-36.png" alt="" class="wp-image-1478155" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-36.png 747w, https://blog.finxter.com/wp-content/uploads/2023/07/image-36-300x202.png 300w" sizes="auto, (max-width: 747px) 100vw, 747px" /><figcaption class="wp-element-caption"><a href="https://upload.wikimedia.org/wikipedia/commons/5/53/Peephole_Long_Short-Term_Memory.svg" data-type="URL" data-id="https://upload.wikimedia.org/wikipedia/commons/5/53/Peephole_Long_Short-Term_Memory.svg" target="_blank" rel="noreferrer noopener">Image source</a></figcaption></figure>
</div>


<ul class="wp-block-list">
<li><strong>Input gate</strong>: The input gate decides how much of the new information should be stored in the cell state. It uses a sigmoid activation function that outputs values between 0 (retain nothing) and 1 (retain everything).</li>



<li><strong>Forget gate</strong>: This gate controls how much of the previous cell state should be forgotten. It also uses a sigmoid activation function. A value close to 0 means forget more, and a value close to 1 means forget less.</li>



<li><strong>Output gate</strong>: The output gate determines what information should be output from the cell state and passed on to the next layer. Its activation function is a combination of sigmoid (for deciding what information to pass) and tanh (to scale the values).</li>
</ul>



<h3 class="wp-block-heading">Vanishing Gradient Problem</h3>



<p>The vanishing gradient problem is a significant challenge in deep learning, especially with RNNs, when dealing with long sequences. During backpropagation, gradients can become extremely small (vanish) or extremely large (explode), making it difficult to train the model effectively. LSTMs manage to mitigate this issue by using their gates, allowing the network to retain relevant information and disregard irrelevant data.</p>



<p>LSTMs, with their unique architecture and gating mechanisms, provide a more robust and effective way of handling sequence data than traditional RNNs. They can capture long-term dependencies and alleviate the vanishing gradient problem, making them suitable for a wide range of applications, such as natural language processing, time series forecasting, and text generation. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> While LSTMs are effective, they are not the only solution for sequence data, as newer models like transformers have emerged to provide alternative approaches for capturing long-range dependencies.</p>



<h2 class="wp-block-heading">Sequence-to-Sequence Models</h2>



<h3 class="wp-block-heading">Seq2Seq</h3>



<p>Seq2Seq (sequence-to-sequence) models are an important breakthrough in the field of Natural Language Processing (NLP). These models are designed to tackle sequence transformation tasks, where the goal is to convert one sequence into another. They consist of two primary components: an <strong>encoder</strong> and a <strong>decoder</strong> network. The encoder processes the input sequence, while the decoder generates the output sequence, typically using recurrent neural networks (RNN), Long Short-Term Memory (LSTM), or Gated Recurrent Units (GRU) to handle the challenge of vanishing gradients <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9e0.png" alt="🧠" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<p>Seq2Seq models have been further improved with the introduction of <strong>attention mechanisms</strong> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f3af.png" alt="🎯" class="wp-smiley" style="height: 1em; max-height: 1em;" />, which allow the model to selectively focus on different parts of the input sequence while generating the output. This greatly enhances the performance of the model, particularly in tasks involving long-range dependencies.</p>



<h3 class="wp-block-heading">NLP Applications</h3>



<p>There are several notable NLP applications that utilize Seq2Seq models, particularly in the area of language translation and neural machine translation <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f310.png" alt="🌐" class="wp-smiley" style="height: 1em; max-height: 1em;" />. These models have proven to be more effective at handling the complexities of language and producing high-quality translations compared to traditional techniques.</p>



<p>Other NLP applications of Seq2Seq models include text summarization, where the model is tasked with generating a shorter, coherent summary of a given document, and conversation modeling to build chatbots <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f916.png" alt="🤖" class="wp-smiley" style="height: 1em; max-height: 1em;" /> that can engage in natural and meaningful dialogues with users.</p>



<h2 class="wp-block-heading">Popular Transformer Models</h2>



<h3 class="wp-block-heading">BERT</h3>



<p><strong>BERT</strong> (Bidirectional Encoder Representations from Transformers) is a prominent transformer model developed by Google AI. It is particularly successful in natural language processing tasks due to its bidirectional encoding capabilities. BERT achieves state-of-the-art results in numerous NLP benchmarks, such as SQuAD, GLUE, and SuperGLUE <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f31f.png" alt="🌟" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<p>This model is pretrained on large datasets, making it easy to fine-tune for specific tasks. BERT comes in different sizes:</p>



<ul class="wp-block-list">
<li>BERT-Base: 12 layers, 768 hidden units</li>



<li>BERT-Large: 24 layers, 1024 hidden units</li>
</ul>



<h3 class="wp-block-heading">GPT</h3>



<p><strong>GPT</strong> (Generative Pre-trained Transformer) is another popular model created by OpenAI. GPT is known for its capacity to generate human-like text, making it suitable for various tasks like text summarization, translation, and question-answering. GPT initially gained attention with its GPT-2 release. Its most recent release, GPT-3, significantly improved in text generation capabilities:</p>



<ul class="wp-block-list">
<li>GPT-3: 175 billion parameters, 96 layers</li>
</ul>



<figure class="wp-block-image size-large"><a href="https://blog.finxter.com/will-gpt-4-save-millions-in-healthcare-radiologists-use-fine-tuned-llms-to-replace-their-jobs/" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="1024" height="564" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-29-1024x564.png" alt="" class="wp-image-1478135" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-29-1024x564.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/07/image-29-300x165.png 300w, https://blog.finxter.com/wp-content/uploads/2023/07/image-29-768x423.png 768w, https://blog.finxter.com/wp-content/uploads/2023/07/image-29.png 1268w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/will-gpt-4-save-millions-in-healthcare-radiologists-use-fine-tuned-llms-to-replace-their-jobs/" data-type="URL" data-id="https://blog.finxter.com/will-gpt-4-save-millions-in-healthcare-radiologists-use-fine-tuned-llms-to-replace-their-jobs/" target="_blank" rel="noreferrer noopener">Will GPT-4 Save Millions in Healthcare? Radiologists Replaced By Fine-Tuned LLMs</a></p>



<h3 class="wp-block-heading">Transformer-XL</h3>



<p><strong>Transformer-XL</strong> (Transformer with extra-long context) is a groundbreaking variant of the original Transformer model. It focuses on overcoming issues in capturing long-range dependencies and enhancing NLP capabilities in tasks like translation and language modeling. Transformer-XL achieves its remarkable performance by implementing a recursive mechanism that connects different segments, allowing the model to efficiently store and access information from previous segments <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<h3 class="wp-block-heading">Vision Transformers</h3>



<p><strong>Vision Transformers</strong> (ViT) are a new category of Transformers, specifically designed for computer vision tasks. ViT models treat an image as a sequence of patches, applying the transformer framework for image classification <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f5bc.png" alt="🖼" class="wp-smiley" style="height: 1em; max-height: 1em;" />. This novel approach challenges the prevalent use of convolutional neural networks (CNNs) for computer vision tasks, achieving state-of-the-art results in benchmarks like ImageNet.</p>



<h2 class="wp-block-heading">Input Representation</h2>



<h3 class="wp-block-heading">Tokenization</h3>



<p>The first step in processing input sequences for both Transformer and LSTM models is tokenization. Tokenization is the process of breaking down the input sentence into smaller units, known as tokens. These tokens are typically words, but can also be subwords or characters, depending on the chosen method. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4d8.png" alt="📘" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Embeddings</h3>



<p>After tokenization, the next step is to convert these tokens into numerical representations that can be fed into the neural networks. This is achieved using embeddings, which map tokens to high-dimensional vectors, often referred to as word vector embeddings. These embeddings capture semantic and syntactic information about the words, allowing the model to understand the relationships between them.</p>



<p>For Transformer models, in addition to word vector embeddings, positional embeddings are also used. Positional embeddings capture the position of each token within the input sequence, as the Transformer architecture processes the entire input simultaneously, contrary to RNNs and LSTMs, which process the input sequentially. Positional encodings, which are calculated using sine and cosine functions, are added to the word vector embeddings, resulting in a combined representation capturing both token meaning and position. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f310.png" alt="🌐" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>In summary:</p>



<ul class="wp-block-list">
<li>Both LSTM and Transformer models require tokenization of input sequences.</li>



<li>Word vector embeddings provide a numerical representation for each token.</li>



<li>Positional embeddings are used in Transformer models to encode positional information.</li>



<li>The final input representation for a Transformer model combines word vector embeddings and positional encodings. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f44d.png" alt="👍" class="wp-smiley" style="height: 1em; max-height: 1em;" /></li>
</ul>



<h2 class="wp-block-heading">Training and Performance</h2>



<h3 class="wp-block-heading">Training Time</h3>



<p>Transformers and LSTMs are both popular choices for training neural networks in deep learning tasks. However, they differ in terms of training time. Transformers are known to have a faster training time compared to LSTMs, as they allow for better parallelization during training. This is because transformers use the self-attention mechanism, which does not rely on sequential computations like LSTMs (<a href="https://voidful.medium.com/why-transformer-faster-then-lstm-on-generation-c3f30977d747">source</a>). Additionally, this parallelization can lead to better utilization of modern GPU architectures, which helps speed up the training process.</p>



<h3 class="wp-block-heading">Efficiency</h3>



<p>When it comes to efficiency, transformers are often more efficient than LSTMs for handling long-range dependencies in sequences. The self-attention mechanism allows transformers to directly access any part of the input sequence, unlike LSTMs, which require processing the sequence step-by-step. Consequently, transformers can better model complex relationships between distant tokens in the input (<a href="https://ai.stackexchange.com/questions/20075/why-does-the-transformer-do-better-than-rnn-and-lstm-in-long-range-context-depen">source</a>).</p>



<p>However, one study comparing transformer and LSTM encoder-decoder models in speech recognition tasks showed that transformers might have a higher tendency to overfit datasets. Despite their stable training process, generalization can still be an issue in some cases (<a href="https://ieeexplore.ieee.org/document/9004025">source</a>).</p>



<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f468-200d-1f4bb.png" alt="👨‍💻" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>TLDR</strong>: Transformers tend to have faster training times and are more efficient at handling long-range dependencies. On the other hand, LSTMs might have fewer generalization issues in certain tasks. It&#8217;s essential to consider these factors when choosing between these neural network architectures for your deep learning project. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f914.png" alt="🤔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Industry Applications and Examples</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="651" src="https://blog.finxter.com/wp-content/uploads/2023/07/image-37-1024x651.png" alt="" class="wp-image-1478156" srcset="https://blog.finxter.com/wp-content/uploads/2023/07/image-37-1024x651.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/07/image-37-300x191.png 300w, https://blog.finxter.com/wp-content/uploads/2023/07/image-37-768x489.png 768w, https://blog.finxter.com/wp-content/uploads/2023/07/image-37.png 1201w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">Machine Translation</h3>



<p>Machine translation is a major application for both LSTM and transformer neural networks. LSTM-based models have been widely used in the past for this task due to their ability to capture the long-range dependencies in languages. However, transformer-based models, such as <a href="https://ai.googleblog.com/2018/11/open-sourcing-bert-state-of-art-pre.html" target="_blank" rel="noreferrer noopener">Google&#8217;s BERT</a>, have recently gained popularity because of their better handling of longer-range contexts and improved performance in translation tasks.</p>



<h3 class="wp-block-heading">Time Series Forecasting</h3>



<p>In the realm of time series forecasting, both LSTM and transformers can be employed to model temporal dependencies present in data. LSTMs have long been a popular choice for their ability to capture both short and long-term dependencies in time series data. On the other hand, transformers can also be used effectively for time series forecasting tasks, thanks to their attention mechanism, which can help identify and focus on important parts of the input sequence.</p>



<h3 class="wp-block-heading">Natural Language Processing</h3>



<p>Natural language processing (NLP) tasks, such as sentiment analysis, text classification, and question-answering, greatly benefit from both LSTM and transformer architectures. LSTMs can handle sequential data like text with relative ease, while transformers&#8217; attention mechanisms allow them to excel in these tasks as well. For instance, transformer-based models like OpenAI&#8217;s GPT-3 have shown remarkable advancements in NLP tasks, setting new benchmarks in the field.</p>



<h3 class="wp-block-heading">Alexa</h3>



<p>Alexa is an example of a voice-controlled virtual assistant developed by Amazon that relies on NLP techniques for understanding and responding to user queries. While the specific architecture used by Alexa is not publicly known, it is likely that a combination of LSTM and transformer models powers its ability to process and generate human-like responses. The advancements in both LSTM and transformer models have considerably contributed to the progress of voice-controlled virtual assistants like Alexa.</p>



<h2 class="wp-block-heading">Challenges and Limitations</h2>



<h3 class="wp-block-heading">Sequential Processing</h3>



<p>Recurrent Neural Networks (RNNs) and Long Short-Term Memory (LSTM) networks are designed to handle sequential data processing, making them suitable for tasks such as time series forecasting <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4c8.png" alt="📈" class="wp-smiley" style="height: 1em; max-height: 1em;" />. However, they can struggle with capturing long-range dependencies in the input data due to their sequential nature. This limitation can make it difficult for them to learn complex patterns over long sequences, especially when the input length becomes significantly longer.</p>



<p>On the other hand, Transformers are specifically designed to overcome this limitation by using self-attention mechanisms that enable them to look at the entire input sequence at once, rather than processing it sequentially. This allows Transformers to efficiently handle long-range dependencies and perform better in various natural language processing tasks.</p>



<h3 class="wp-block-heading">Memory Constraints</h3>



<p>Although Transformers are known for their ability to handle long-range dependencies, they can also come at a cost in terms of computational resources and memory usage. They typically require more memory and computation power compared to RNNs and LSTMs due to their extensive use of self-attention mechanisms.<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f680.png" alt="🚀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>For instance, using Transformers on mobile or embedded devices with limited CPU and memory can be challenging. Moreover, training deep Transformers can demand a significant amount of computational resources, which might not always be feasible depending on the application&#8217;s infrastructure.</p>



<p>While Transformers are powerful models that can outperform RNNs and LSTMs in various tasks, they are not without their challenges and limitations. The choice of model should be made considering the specific requirements and constraints of the problem at hand.<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Frequently Asked Questions</h2>



<h3 class="wp-block-heading">What are the key differences between LSTM and Transformer?</h3>



<p>Long Short-Term Memory (LSTM) and Transformers are two types of neural networks designed for sequence-based tasks like natural language processing. LSTM is a type of Recurrent Neural Network (RNN) that addresses the vanishing gradient problem, enabling it to capture longer dependencies in sequences. Meanwhile, Transformers utilize self-attention mechanisms to process sequence inputs, handling long-range dependencies more efficiently. The most notable difference is the absence of RNN cells in Transformer architecture, which allows it to process inputs in parallel, resulting in faster computation <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f680.png" alt="🚀" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<h3 class="wp-block-heading">How do LSTM and Transformer models compare in terms of speed and performance?</h3>



<p>In terms of performance, both LSTM and Transformer models can deliver impressive results. However, Transformers have been shown to outperform LSTMs in some tasks, particularly those involving longer input sequences. With respect to speed, Transformers have a significant advantage, as they can process sequence data in parallel, enabling faster training and inference times <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/23f1.png" alt="⏱" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<h3 class="wp-block-heading">Are Transformers more suitable for certain tasks compared to LSTMs?</h3>



<p>Transformers are considered to excel in tasks that require long-range dependencies handling and parallel processing, such as machine translation, text summarization, and natural language understanding. LSTMs can still perform well on shorter sequences, and they may be more suitable for tasks that do not require the full power and complexity of a Transformer model, like sentiment analysis or time-series prediction <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f52e.png" alt="🔮" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<h3 class="wp-block-heading">Why might one choose Transformer over LSTM in specific applications?</h3>



<p>Choosing a Transformer model over LSTM could be motivated by several factors, such as:</p>



<ol class="wp-block-list">
<li>Parallel processing capabilities: Transformers can process sequence data concurrently, which is beneficial for computational efficiency and shorter training times.</li>



<li>Long-range dependency handling: Transformers excel at understanding dependencies across larger sequences, making them ideal for complex tasks like machine translation or text summarization.</li>



<li>Scalability: Due to their parallel processing capability, Transformers can handle larger input sequences more effectively than LSTMs <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f310.png" alt="🌐" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</li>
</ol>



<h3 class="wp-block-heading">Can LSTM and Transformer models be combined effectively?</h3>



<p>Yes, LSTM and Transformer models can be combined effectively in hybrid architectures, taking advantage of the strengths of both approaches. One example is the use of an LSTM layer for capturing local dependencies within a Transformer-based network, exploiting the flexibility of deep learning frameworks to design custom solutions tailored to specific tasks <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f9e0.png" alt="🧠" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<h3 class="wp-block-heading">How do the architectures of LSTM and Transformer models differ?</h3>



<p>LSTM models consist of RNN cells with a specialized internal structure, designed to store and manipulate information across time steps more efficiently. In contrast, Transformer models contain a stack of encoder and decoder layers, each consisting of self-attention and feed-forward neural network components. This architecture allows Transformers to process sequence data without the need for recurrent connections or cells, enabling parallel processing and more efficient long-range dependency handling <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f50d.png" alt="🔍" class="wp-smiley" style="height: 1em; max-height: 1em;" />.</p>



<h2 class="wp-block-heading">Prompt Engineering with Python and OpenAI</h2>



<figure class="wp-block-image size-full"><a href="https://academy.finxter.com/university/prompt-engineering-with-python-and-openai/" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="799" height="350" src="https://blog.finxter.com/wp-content/uploads/2023/06/image-288.png" alt="" class="wp-image-1463464" srcset="https://blog.finxter.com/wp-content/uploads/2023/06/image-288.png 799w, https://blog.finxter.com/wp-content/uploads/2023/06/image-288-300x131.png 300w, https://blog.finxter.com/wp-content/uploads/2023/06/image-288-768x336.png 768w" sizes="auto, (max-width: 799px) 100vw, 799px" /></a></figure>



<p>You can check out the whole <a href="https://academy.finxter.com/university/prompt-engineering-with-python-and-openai/" data-type="URL" data-id="https://academy.finxter.com/university/prompt-engineering-with-python-and-openai/" target="_blank" rel="noreferrer noopener">course on OpenAI Prompt Engineering using Python on the Finxter academy</a>. We cover topics such as:</p>



<ul class="wp-block-list">
<li>Embeddings</li>



<li>Semantic search</li>



<li>Web scraping</li>



<li>Query embeddings</li>



<li>Movie recommendation</li>



<li>Sentiment analysis</li>
</ul>



<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f468-200d-1f4bb.png" alt="👨‍💻" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Academy</strong>: <a href="https://academy.finxter.com/university/prompt-engineering-with-python-and-openai/" data-type="URL" data-id="https://academy.finxter.com/university/prompt-engineering-with-python-and-openai/" target="_blank" rel="noreferrer noopener">Prompt Engineering with Python and OpenAI</a></p>
<p>The post <a href="https://blog.finxter.com/transformer-vs-lstm/">Transformer vs LSTM: A Helpful Illustrated Guide</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Python Int to String with Leading Zeros</title>
		<link>https://blog.finxter.com/python-int-to-string-with-leading-zeros/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Sat, 25 Feb 2023 13:12:28 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Productivity]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Python List]]></category>
		<category><![CDATA[Python One-Liners]]></category>
		<category><![CDATA[Python String]]></category>
		<category><![CDATA[Scripting]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=16851</guid>

					<description><![CDATA[<p>To convert an integer i to a string with leading zeros so that it consists of 5 characters, use the format string f'{i:05d}'. The d flag in this expression defines that the result is a decimal value. The str(i).zfill(5) accomplishes the same string conversion of an integer with leading zeros. Challenge: Given an integer number. ... <a title="Python Int to String with Leading Zeros" class="read-more" href="https://blog.finxter.com/python-int-to-string-with-leading-zeros/" aria-label="Read more about Python Int to String with Leading Zeros">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/python-int-to-string-with-leading-zeros/">Python Int to String with Leading Zeros</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-pale-cyan-blue-background-color has-background">To convert an integer <code>i</code> to a string with leading zeros so that it consists of <code>5</code> characters, use the format string <code>f'{i:05d}'</code>. The <code>d</code> flag in this expression defines that the result is a decimal value. The <code>str(i).zfill(5)</code> accomplishes the same string conversion of an integer with leading zeros. </p>



<p><strong>Challenge</strong>: Given an integer number. How to convert it to a string by adding leading zeros so that the string has a fixed number of positions. </p>



<p><strong>Example</strong>: For integer 42, you want to fill it up with leading zeros to the following string with 5 characters: <code>'00042'</code>. </p>



<p><em>In all methods, we assume that the integer has less than 5 characters. </em></p>



<h2 class="wp-block-heading">Method 1: Format String</h2>



<p>The first method uses the <a rel="noreferrer noopener" title="https://docs.python.org/3/library/string.html#format-string-syntax" href="https://docs.python.org/3/library/string.html#format-string-syntax" target="_blank">format string feature</a> in Python 3+ called f-strings or <em>replacement fields</em>. </p>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Info</strong>: In Python, f-strings allow for the embedding of expressions within strings by prefixing a string with the letter <code>"f"</code> or <code>"F"</code> and enclosing expressions within curly braces <code>{}</code>. The expressions within the curly braces in the f-string are evaluated, and their values are inserted into the resulting string. This allows for a concise and readable way to include variable values or complex expressions within string literals.</p>



<p>The following f-string converts an integer <code>i</code> to a string while adding leading zeros to a given integer:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="6" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Integer value to be converted
i = 42


# Method 1: Format String
s1 = f'{i:05d}'
print(s1)
# 00042</pre>



<p>The code <code data-enlighter-language="generic" class="EnlighterJSRAW">f'{i:05d}'</code> places the integer <code>i</code> into the newly created string. However, it tells the format language to fill the string to <code>5</code> characters with leading <code>'0'</code>s using the decimal system. </p>



<p>This is the most Pythonic way to accomplish this challenge. </p>



<h2 class="wp-block-heading">Method 2: zfill()</h2>



<p>Another readable and Pythonic way to fill the string with leading 0s is the <code><a href="https://blog.finxter.com/python-pad-zeros-to-a-string/" target="_blank" rel="noreferrer noopener" title="https://blog.finxter.com/python-pad-zeros-to-a-string/">string.zfill()</a></code> method. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Method 2: zfill()
s2 = str(i).zfill(5)
print(s2)
# 00042</pre>



<p>The method takes one argument and that is the number of positions of the resulting string. Per default, it fills with 0s. </p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img loading="lazy" decoding="async" src="https://blog.finxter.com/wp-content/uploads/2020/11/padString-1024x576.jpg" alt="Python How to Pad Zeros to a String?" class="wp-image-16905" style="width:768px;height:432px" width="768" height="432" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/padString-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2020/11/padString-300x169.jpg 300w, https://blog.finxter.com/wp-content/uploads/2020/11/padString-768x432.jpg 768w, https://blog.finxter.com/wp-content/uploads/2020/11/padString-150x84.jpg 150w" sizes="auto, (max-width: 768px) 100vw, 768px" /></figure>
</div>


<p>You can check out the following video tutorial from Finxter <strong>Adam</strong>:</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python How to Pad Zeros to a String Video" width="937" height="703" src="https://www.youtube.com/embed/0F-qu-vkPYA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<h2 class="wp-block-heading">Method 3: List Comprehension</h2>



<p>Many Python coders don&#8217;t quite get the f-strings and the <code>zfill()</code> method shown in Methods 2 and 3. If you don&#8217;t have time learning them, you can also use a more standard way based on <a rel="noreferrer noopener" title="Daily Python Puzzle: String Concatenation" href="https://blog.finxter.com/daily-python-puzzle-string-concatenation/" target="_blank">string concatenation</a> and <a rel="noreferrer noopener" title="List Comprehension in Python — A Helpful Illustrated Guide" href="https://blog.finxter.com/list-comprehension/" target="_blank">list comprehension</a>. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Method 3: List Comprehension
s3 = str(i)
n = len(s3)
s3 = '0' * (5-len(s3)) + s3
print(s3)
</pre>



<p>You first convert the integer to a basic string. Then, you create the prefix of <code>0</code>s, you need to fill it up to <code>n=5</code> characters and concatenate it to the integer&#8217;s string representation. The <a title="What is the Asterisk / Star Operator (*) in Python?" href="https://blog.finxter.com/what-is-asterisk-in-python/" target="_blank" rel="noreferrer noopener">asterisk operator</a> creates a string of <code>5-len(s3)</code> zeros here. </p>



<p>If you want to learn how to add <em>trailing </em>zeros instead of leading zeros, check out <a href="https://blog.finxter.com/python-int-to-string-with-trailing-zeros/">this article on the Finxter blog</a>.</p>



<h2 class="wp-block-heading">Where to Go From Here?</h2>



<p>Enough theory. Let’s get some practice!</p>



<p>Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. </p>



<p>To become more successful in coding, solve more real problems for real people. 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>You build high-value coding skills by working on practical coding projects!</strong></p>



<p>Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?</p>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f680.png" alt="🚀" class="wp-smiley" style="height: 1em; max-height: 1em;" /> If your answer is <strong><em>YES!</em></strong>, consider becoming a <a rel="noreferrer noopener" href="https://blog.finxter.com/become-python-freelancer-course/" data-type="page" data-id="2072" target="_blank">Python freelance developer</a>! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>



<p>If you just want to learn about the freelancing opportunity, feel free to watch 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 learn 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>



<h2 class="wp-block-heading">Programmer Humor</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="925" height="507" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-9.png" alt="" class="wp-image-401703" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-9.png 925w, https://blog.finxter.com/wp-content/uploads/2022/06/image-9-300x164.png 300w, https://blog.finxter.com/wp-content/uploads/2022/06/image-9-768x421.png 768w" sizes="auto, (max-width: 925px) 100vw, 925px" /><figcaption><em>&#8220;Real programmers set the universal constants at the start such that the universe evolves to contain the disk with the data they want.&#8221;</em> &#8212; <a rel="noreferrer noopener" href="https://imgs.xkcd.com/comics/real_programmers.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/real_programmers.png" target="_blank">xkcd</a></figcaption></figure>
</div><p>The post <a href="https://blog.finxter.com/python-int-to-string-with-leading-zeros/">Python Int to String with Leading Zeros</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How To Extract Numbers From A String In Python?</title>
		<link>https://blog.finxter.com/how-to-extract-numbers-from-a-string-in-python/</link>
		
		<dc:creator><![CDATA[Shubham Sayon]]></dc:creator>
		<pubDate>Fri, 24 Feb 2023 22:59:10 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Data Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Python List]]></category>
		<category><![CDATA[Python One-Liners]]></category>
		<category><![CDATA[Python String]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[Research]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=13007</guid>

					<description><![CDATA[<p>The easiest way to extract numbers from a Python string s is to use the expression re.findall('\d+', s). For example, re.findall('\d+', 'hi 100 alice 18 old 42') yields the list of strings ['100', '18', '42'] that you can then convert to numbers using int() or float(). There are some tricks and alternatives, so keep reading ... <a title="How To Extract Numbers From A String In Python?" class="read-more" href="https://blog.finxter.com/how-to-extract-numbers-from-a-string-in-python/" aria-label="Read more about How To Extract Numbers From A String In Python?">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/how-to-extract-numbers-from-a-string-in-python/">How To Extract Numbers From A String In Python?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-global-color-8-background-color has-background">The easiest way to extract numbers from a Python string <code>s</code> is to use the expression <code>re.findall('\d+', s)</code>. For example, <code>re.findall('\d+', 'hi 100 alice 18 old 42')</code> yields the list of strings <code>['100', '18', '42']</code> that you can then convert to numbers using <code>int()</code> or <code>float()</code>.</p>



<p>There are some tricks and alternatives, so keep reading to learn about them. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>In particular, you&#8217;ll learn about the following methods to extract numbers from a given string in Python:</p>



<ul class="wp-block-list">
<li>Use the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex/" target="_blank">regex</a></code> module.</li>



<li>Use <code><a href="https://blog.finxter.com/python-string-split/" data-type="post" data-id="26097">split()</a></code> and <code><a href="https://blog.finxter.com/python-list-append/" data-type="post" data-id="6605" target="_blank" rel="noreferrer noopener">append()</a></code> functions on a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-methods/" target="_blank">list</a>.</li>



<li>Use a <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/#:~:text=%E2%80%9CA%20list%20comprehension%20consists%20of,if%20clauses%20which%20follow%20it.%E2%80%9D&amp;text=In%20other%20words%2C%20here%20is%20the%20formula%20for%20list%20comprehension." target="_blank">List Comprehension</a> with <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-isdigit/" data-type="post" data-id="26044" target="_blank">isdigit()</a></code> and <code><a href="https://blog.finxter.com/python-string-split/" data-type="post" data-id="26097" target="_blank" rel="noreferrer noopener">split()</a></code> functions.</li>



<li>Use the <code>num_from_string</code> module.</li>
</ul>



<h2 class="wp-block-heading">Problem Formulation</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="963" height="587" src="https://blog.finxter.com/wp-content/uploads/2023/02/image-346.png" alt="" class="wp-image-1160759" srcset="https://blog.finxter.com/wp-content/uploads/2023/02/image-346.png 963w, https://blog.finxter.com/wp-content/uploads/2023/02/image-346-300x183.png 300w, https://blog.finxter.com/wp-content/uploads/2023/02/image-346-768x468.png 768w" sizes="auto, (max-width: 963px) 100vw, 963px" /></figure>
</div>


<p><em>Extracting digits or numbers from a given string might come up in your coding journey quite often. For instance, you may want to extract certain numerical figures from a CSV file, or you need to separate complex digits and figures from given patterns. </em></p>



<p>Having said that, let us dive into our mission-critical question: </p>



<p><strong>Problem: </strong>Given a string. How to extract numbers from the string in Python?</p>



<p><strong>Example: </strong>Consider that you have been given a string and you want to extract all the numbers from the string as given in the following example:</p>



<p>Given is the following string: </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">s = 'Extract 100, 1000 and 10000 from this string'</pre>



<p>This is your desired 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="">[100, 1000, 10000]</pre>



<p>Let us discuss the methods that we can use to extract the numbers from the given string: </p>



<h2 class="wp-block-heading">Method 1: Using Regex Module</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="588" height="879" src="https://blog.finxter.com/wp-content/uploads/2023/02/image-347.png" alt="" class="wp-image-1160760" srcset="https://blog.finxter.com/wp-content/uploads/2023/02/image-347.png 588w, https://blog.finxter.com/wp-content/uploads/2023/02/image-347-201x300.png 201w" sizes="auto, (max-width: 588px) 100vw, 588px" /></figure>
</div>


<p>The most efficient approach to solving our problem is to leverage the power of the <code>re</code> module. You can easily use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex/" data-type="post" data-id="6210" target="_blank">Regular Expressions</a> (<code>RegEx</code>) to check or verify if a given string contains a specified pattern (be it a digit or a special character, or any other pattern). </p>



<p>Thus to solve our problem, we must import the <code>regex</code> module, which is already included in Python&#8217;s standard library, and then with the help of the <code>findall()</code> function we can extract the numbers from the given string. </p>



<p class="has-base-background-color has-background">◈  <strong>Learn More</strong>: <code>re.findall()</code> is an easy-to-use regex function that returns a list containing all matches. To learn more about <code>re.findall()</code> check out our <a rel="noreferrer noopener" title="Python re.findall() – Everything You Need to Know" href="https://blog.finxter.com/python-re-findall/" target="_blank">blog tutorial here.</a></p>



<p>Let us have a look at the following code to understand how we can use the <code>regex</code> module to solve our problem:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re

sentence = 'Extract 100 , 100.45 and 10000 from this string'
s = [float(s) for s in re.findall(r'-?\d+\.?\d*', sentence)]
print(s)</pre>



<p><strong>Output</strong></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="">[100.0, 100.45, 10000.0]</pre>



<p>This is a Python code that uses the <code>re</code> module, which provides support for regular expressions in Python, to extract numerical values from a string. </p>



<p><strong>Code explanation:</strong> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>The line <code>s = [float(s) for s in re.findall(r'-?\d+\.?\d*', sentence)]</code> uses the <code>re.findall()</code> function from the <code>re</code> module to search the <code>sentence</code> string for numerical values. </p>



<p>Specifically, it looks for strings of characters that match the regular expression pattern <code>r'-?\d+.?\d*'</code>. This pattern matches an optional minus sign, followed by one or more digits, followed by an optional decimal point, followed by zero or more digits. </p>



<p>The <code>re.findall()</code> function returns a list of all the matching strings.</p>



<p>The list comprehension <code>[float(s) for s in re.findall(r'-?\d+\.?\d*', sentence)]</code> takes the list of matching strings returned by <code>findall</code> and converts each string to a floating-point number using the <code>float()</code> function. This resulting list of floating-point numbers is then assigned to the variable <code>s</code>.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/list-comprehension/" data-type="post" data-id="1171" target="_blank" rel="noreferrer noopener">Python List Comprehension</a></p>



<h2 class="wp-block-heading">Method 2: Split and Append The Numbers To A List using split() and append()</h2>



<p>Another workaround for our problem is to split the given string using the <code>split()</code> function and then extract the numbers using the built-in <code>float()</code> method then append the extracted numbers to the <a href="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener" title="The Ultimate Guide to Python Lists">list</a>. </p>



<p><strong>Note:</strong></p>



<ul class="wp-block-list">
<li><code><a href="https://blog.finxter.com/how-to-split-a-list-into-evenly-sized-chunks/" target="_blank" rel="noreferrer noopener" title="How to Split a List Into Evenly-Sized Chunks?">split()</a></code> is a built-in python method which is used to split a string into a list.</li>



<li><code><a href="https://blog.finxter.com/python-list-append/" title="Python List append() Method">append()</a></code> is a built-in method in python that adds an item to the end of a list. </li>
</ul>



<p>Now that we have the necessary tools to solve our problem based on the above concept let us dive into the code to see how it works:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sentence = 'Extract 100 , 100.45 and 10000 from this string'

s = []
for t in sentence.split():
    try:
        s.append(float(t))
    except ValueError:
        pass
print(s)</pre>



<p><strong>Output</strong></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="">[100.0, 100.45, 10000.0]</pre>



<h2 class="wp-block-heading">Method 3: Using isdigit() Function In A List Comprehension</h2>



<p>Another approach to solving our problem is to use the <code>isdigit()</code> inbuilt function to extract the digits from the string and then store them in a list using a <a href="https://blog.finxter.com/how-does-nested-list-comprehension-work-in-python/">list comprehension</a>. </p>



<p>The <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-isdigit/" data-type="post" data-id="26044" target="_blank">isdigit()</a></code> function is used to check if a given string contains digits. Thus if it finds a character that is a digit, then it returns <code>True</code>. Otherwise, it returns <code>False</code>.</p>



<p>Let us have a look at the code given below to see how the above concept works:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sentence = 'Extract 100 , 100.45 and 10000 from this string'
s = [int(s) for s in str.split(sentence) if s.isdigit()]
print(s)</pre>



<p><strong>Output</strong></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="">[100, 10000]</pre>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2622.png" alt="☢" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Alert</strong>! This technique is best suited to extract only positive integers. It won&#8217;t work for negative integers, floats, or hexadecimal numbers.   </p>



<h2 class="wp-block-heading">Method 4:  Using Numbers from String Library</h2>



<p>This is a quick hack if you want to avoid spending time typing explicit code to extract numbers from a string. </p>



<p>You can import a library known as <code>nums_from_string</code> and then use it to extract numbers from a given string. It contains several regex rules with comprehensive coverage and can be a very useful tool for NLP researchers.</p>



<p>Since the <code>nums_from_string</code> library is not a part of the standard Python library, you have to install it before use. Use the following command to install this useful library:</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="">pip install nums_from_string</pre>



<p>The following program demonstrates the usage of <code>nums_from_string</code> :</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import nums_from_string

sentence = 'Extract 100 , 100.45 and 10000 from this string'
print(nums_from_string.get_nums(sentence))</pre>



<p><strong>Output</strong></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="">[100.0, 100.45, 10000.0]</pre>



<h2 class="wp-block-heading">Conclusion</h2>



<p>Thus from the above discussions, we found that there are numerous ways of extracting a number from a given string in python. </p>



<p>My personal favorite, though, would certainly be the regex module <code>re</code>. </p>



<p>You might argue that using other methods like the <code>isdigit()</code> and <code>split()</code> functions provide simpler and more readable code and faster. However, as mentioned earlier, it does not return numbers that are negative (in reference to Method 2) and also does not work for floats that have no space between them and other characters like <code>'25.50k'</code> (in reference to Method 2). </p>



<p>Furthermore, speed is kind of an irrelevant metric when it comes to log parsing. Now you see why regex is my personal favorite in this list of solutions. </p>



<p>If you are not very supportive of the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/re.html" target="_blank"><code>re</code> library</a>, especially because you find it difficult to get a strong grip on this concept (just like me in the beginning), here&#8217;s <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex/" target="_blank">THE TUTORIAL</a> for you to become a regex master. <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92f.png" alt="🤯" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92f.png" alt="🤯" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>I hope you found this article useful and added some value to your coding journey. Please <a rel="noreferrer noopener" href="https://blog.finxter.com/subscribe" target="_blank">stay tuned</a> for more interesting stuff in the future. </p>



<p>The post <a href="https://blog.finxter.com/how-to-extract-numbers-from-a-string-in-python/">How To Extract Numbers From A String In Python?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>I Created a Crypto Arbitrage Trading Bot With Python</title>
		<link>https://blog.finxter.com/i-created-a-crypto-arbitrage-trading-bot-with-python/</link>
		
		<dc:creator><![CDATA[Emily Rosemary Collins]]></dc:creator>
		<pubDate>Tue, 17 Jan 2023 16:40:03 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Crypto]]></category>
		<category><![CDATA[Finance]]></category>
		<category><![CDATA[Investment]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Trading]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=1065686</guid>

					<description><![CDATA[<p>🛑 Disclaimer: NOT INVESTMENT ADVICE! In this short project, I&#8217;ll explain a Python trading bot I used for the purpose of arbitrage trading. I use Bitcoin BTC, but the arbitrage bot works better on illiquid and inefficiently priced coins &#8212; Bitcoin is usually far too liquid and efficiently priced for this to work. I also ... <a title="I Created a Crypto Arbitrage Trading Bot With Python" class="read-more" href="https://blog.finxter.com/i-created-a-crypto-arbitrage-trading-bot-with-python/" aria-label="Read more about I Created a Crypto Arbitrage Trading Bot With Python">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/i-created-a-crypto-arbitrage-trading-bot-with-python/">I Created a Crypto Arbitrage Trading Bot With Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f6d1.png" alt="🛑" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Disclaimer</strong>: NOT INVESTMENT ADVICE!</p>



<p>In this short project, I&#8217;ll explain a Python trading bot I used for the purpose of arbitrage trading. </p>



<p>I use Bitcoin BTC, but the arbitrage bot works better on illiquid and inefficiently priced coins &#8212; Bitcoin is usually far too liquid and efficiently priced for this to work. I also assume an exchange rate of 1 GBP > 1 EUR > 1 USD. </p>



<h2 class="wp-block-heading">How Does It Work?</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="587" height="881" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-162.png" alt="" class="wp-image-1065746" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-162.png 587w, https://blog.finxter.com/wp-content/uploads/2023/01/image-162-200x300.png 200w" sizes="auto, (max-width: 587px) 100vw, 587px" /></figure>
</div>


<p>The Bitcoin arbitrage bot continuously checks the prices of Bitcoin and looks for very simple, almost trivial arbitrage opportunities. </p>



<ul class="wp-block-list">
<li>If you can sell Bitcoin at a EUR price above the USD price and you assume EUR is worth more than USD, you buy Bitcoin for USD and sell it for EUR.</li>



<li>If you can sell Bitcoin at a GBP price above the USD price and you assume GBP is worth more than USD, you buy Bitcoin for USD and sell it for GBP.</li>



<li>If you can sell Bitcoin at a GBP price above the EUR price and you assume GBP is worth more than EUR, you buy Bitcoin for EUR and sell it for GBP.</li>
</ul>



<p>When these trivial opportunities are found, the bot will execute the appropriate trade to take advantage of the price difference. </p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Info</strong>: Don&#8217;t expect this to work for highly liquid and efficiently priced trading pairs &#8212; but it may work for inefficiently priced trading pairs. That&#8217;s where all the arbitrage opportunities are! </p>



<p>I have actually replaced the execution of the concrete trade with a simple <code><a href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank" rel="noreferrer noopener">print()</a></code> statement to make it more generalized &#8212; no matter which trading tool you&#8217;re actually using.</p>



<p>The loop is set to sleep for 60 seconds before checking for arbitrage opportunities again.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import time
import requests 
import pandas as pd
import numpy as np

def get_price_data():
    '''
    Get the latest crypto price data from various exchanges
    '''
    url = 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC&amp;tsyms=USD,EUR,GBP'
    resp = requests.get(url=url)
    data = resp.json()
    return data

def calculate_arbitrage(data):
    '''
    Calculate the arbitrage opportunity between exchanges
    '''
    # Get the exchange prices
    btc_usd = data['BTC']['USD']
    btc_eur = data['BTC']['EUR']
    btc_gbp = data['BTC']['GBP']
    # Calculate the arbitrage opportunity
    usd_eur_diff = btc_eur - btc_usd
    usd_gbp_diff = btc_gbp - btc_usd
    eur_gbp_diff = btc_gbp - btc_eur
    
    return usd_eur_diff, usd_gbp_diff, eur_gbp_diff

def trade_arbitrage(usd_eur_diff, usd_gbp_diff, eur_gbp_diff):
    '''
    Execute an arbitrage trade
    '''
    if usd_eur_diff > 0:
        # Buy BTC with USD and sell it for EUR
        # Profit from USD->EUR difference
        print('Executing USD->EUR arbitrage trade')
    elif usd_gbp_diff > 0:
        # Buy BTC with USD and sell it for GBP
        # Profit from USD->GBP difference
        print('Executing USD->GBP arbitrage trade')
    elif eur_gbp_diff > 0:
        # Buy BTC with EUR and sell it for GBP
        # Profit from EUR->GBP difference
        print('Executing EUR->GBP arbitrage trade')
    else:
        print('No arbitrage opportunity.')

if __name__ == '__main__':
    while True:
        data = get_price_data()
        usd_eur_diff, usd_gbp_diff, eur_gbp_diff = calculate_arbitrage(data)
        trade_arbitrage(usd_eur_diff, usd_gbp_diff, eur_gbp_diff)
        time.sleep(60)
</pre>



<p>This code is a Python program that implements a Bitcoin arbitrage trading bot. It works by continually checking the prices of Bitcoin on various exchanges and looking for arbitrage opportunities.</p>



<h2 class="wp-block-heading">Detailed Explanation</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="683" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-163-1024x683.png" alt="" class="wp-image-1065750" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-163-1024x683.png 1024w, https://blog.finxter.com/wp-content/uploads/2023/01/image-163-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/01/image-163-768x512.png 768w, https://blog.finxter.com/wp-content/uploads/2023/01/image-163.png 1113w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>The program starts by importing the necessary libraries, such as <code><a href="https://blog.finxter.com/python-requests-library-2/" data-type="post" data-id="37804" target="_blank" rel="noreferrer noopener">requests</a></code>, <code><a href="https://blog.finxter.com/pandas-quickstart/" data-type="post" data-id="16511" target="_blank" rel="noreferrer noopener">pandas</a></code>, and <code><a href="https://blog.finxter.com/numpy-tutorial/" data-type="post" data-id="1356" target="_blank" rel="noreferrer noopener">numpy</a></code>.</p>



<p>Next, you define the function <code>get_price_data()</code> that uses the <a rel="noreferrer noopener" href="https://min-api.cryptocompare.com/documentation" data-type="URL" data-id="https://min-api.cryptocompare.com/documentation" target="_blank">CryptoCompare API</a> to retrieve the latest prices of Bitcoin from various exchanges. The data is returned in JSON format.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(data)
{'BTC': {'USD': 21149.49, 'EUR': 19603.81, 'GBP': 17252.73}}</pre>



<p>You define the <code>calculate_arbitrage()</code> that takes in the data returned by <code>get_price_data()</code> and calculates the arbitrage opportunities between the different exchanges. </p>



<p>It returns the differences in the prices of Bitcoin in USD-EUR, USD-GBP, and EUR-GBP.</p>



<p>You define the <code>trade_arbitrage()</code> function that takes in the arbitrage opportunities calculated by the previous function and executes the appropriate trade based on the opportunity. </p>



<ul class="wp-block-list">
<li>If the USD-EUR arbitrage opportunity is positive, the bot will buy Bitcoin with USD and sell it for EUR. </li>



<li>If the USD-GBP arbitrage opportunity is positive, the bot will buy Bitcoin with USD and sell it for GBP. </li>



<li>If the EUR-GBP arbitrage opportunity is positive, the bot will buy Bitcoin with EUR and sell it for GBP.</li>
</ul>



<p>Finally, the program enters an infinite loop where it continuously checks for arbitrage opportunities and executes trades when necessary. The loop is set to sleep for 60 seconds before checking for arbitrage opportunities again.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f449.png" alt="👉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/how-to-stop-a-while-loop-in-python/" data-type="post" data-id="29478" target="_blank" rel="noreferrer noopener">How to Stop an Infinite While Loop in Python</a></p>
<p>The post <a href="https://blog.finxter.com/i-created-a-crypto-arbitrage-trading-bot-with-python/">I Created a Crypto Arbitrage Trading Bot With Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Python Matplotlib Makes Conway&#8217;s Game of Life Come Alive</title>
		<link>https://blog.finxter.com/python-matplotlib-makes-conways-game-of-life-come-alive/</link>
		
		<dc:creator><![CDATA[Emily Rosemary Collins]]></dc:creator>
		<pubDate>Mon, 12 Dec 2022 12:08:32 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Visualization]]></category>
		<category><![CDATA[Matplotlib]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=972637</guid>

					<description><![CDATA[<p>In this article, you&#8217;ll learn how to make this beautiful and interesting animation using only Python, NumPy, and Matplotlib &#8212; nothing else: 👇 But how does the Game of Life work &#8211; and what&#8217;s behind the classical visualization anyways? The Game of Life Conway&#8217;s Game of Life is a cellular automaton devised by the British ... <a title="Python Matplotlib Makes Conway&#8217;s Game of Life Come Alive" class="read-more" href="https://blog.finxter.com/python-matplotlib-makes-conways-game-of-life-come-alive/" aria-label="Read more about Python Matplotlib Makes Conway&#8217;s Game of Life Come Alive">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/python-matplotlib-makes-conways-game-of-life-come-alive/">Python Matplotlib Makes Conway&#8217;s Game of Life Come Alive</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, you&#8217;ll learn how to make this beautiful and interesting animation using only Python, NumPy, and Matplotlib &#8212; nothing else: <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f447.png" alt="👇" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="790" height="569" src="https://blog.finxter.com/wp-content/uploads/2022/12/game_of_life_python.gif" alt="" class="wp-image-972677"/></figure>



<p>But how does the Game of Life work &#8211; and what&#8217;s behind the classical visualization anyways?</p>



<h2 class="wp-block-heading">The Game of Life</h2>



<p>Conway&#8217;s <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" data-type="URL" data-id="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank" rel="noreferrer noopener">Game of Life</a> is a cellular automaton devised by the British mathematician <em>John Horton Conway</em> in 1970. The game is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input.</p>



<p>The game is played on a two-dimensional grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. </p>



<p>At each step in time, the following rules apply:</p>



<ol class="has-base-background-color has-background wp-block-list">
<li>Any live cell with fewer than two live neighbors dies, as if by underpopulation.</li>



<li>Any live cell with two or three live neighbors lives on to the next generation.</li>



<li>Any live cell with more than three live neighbors dies, as if by overpopulation.</li>



<li>Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.</li>
</ol>



<p>The initial pattern constitutes the seed of the system. </p>



<p>The first generation is created by applying the above rules simultaneously to every cell in the seed; births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a <em><strong>tick</strong></em>. </p>



<p>Each generation is a pure function of the preceding one. The rules continue to be applied repeatedly to create further generations.</p>



<h2 class="wp-block-heading">How to Implement the &#8220;Game of Life&#8221; in Python?</h2>



<p>This code creates a universe of size <code>NxN</code> with a probability <code>p</code> of being populated. </p>



<p>It then animates the universe for 200 frames with a 200ms interval between each frame, and displays it using Matplotlib. </p>



<p>The rules of the animation are defined in the <code>animate()</code> function, which iterates over each cell in the universe and applies the rules of Conway&#8217;s Game of Life.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation


def create_universe(N=50, p=0.5):
    return np.random.choice([0, 1], size=(N, N), p=[1-p, p])


def animate(frame, universe, img):
    new_u = np.zeros((N, N))
    for i in range(N):
        for j in range(N):
            n = (universe[(i+1)%N][j] + universe[(i-1)%N][j]
                 + universe[i][(j+1)%N] + universe[i][(j-1)%N]
                 + universe[(i+1)%N][(j+1)%N] + universe[(i-1)%N][(j-1)%N]
                 + universe[(i+1)%N][(j-1)%N] + universe[(i-1)%N][(j+1)%N])
            if universe[i][j] == 0 and n == 3:
                new_u[i][j] = 1
            elif universe[i][j] == 1 and (n &lt; 2 or n > 3):
                new_u[i][j] = 0
            else:
                new_u[i][j] = universe[i][j]
    img.set_data(new_u)
    universe[:] = new_u[:]
    return img


N = 50
universe = create_universe(N=N, p=0.5)
fig = plt.figure(figsize=(7, 7))
ax = plt.axes()
img = ax.imshow(universe, interpolation='nearest')
ani = FuncAnimation(fig, animate, fargs=(universe, img,),
                    frames=200, interval=200, save_count=50)
plt.show()
</pre>



<h2 class="wp-block-heading">Quick Code Explanation</h2>



<p>The Python code creates an animation of Conway&#8217;s hugely famous &#8220;Game of Life&#8221;.  </p>



<p>First, you import the <a rel="noreferrer noopener" href="https://blog.finxter.com/numpy-tutorial/" data-type="post" data-id="1356" target="_blank">NumPy</a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/matplotlib-full-guide/" data-type="post" data-id="20151" target="_blank">Matplotlib</a> libraries. </p>



<p>Second, you define a <code>create_universe()</code> function that takes two parameters and returns a random array of 0&#8217;s and 1&#8217;s. </p>



<p>Third, you create the <code>animate()</code> function that uses a <code>for</code> loop to <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-iterate-over-a-numpy-array/" data-type="post" data-id="555100" target="_blank">iterate</a> through the array and update each cell using the &#8220;Game of live rules&#8221; discussed above.</p>



<p>Fourth, the animation is displayed with the <code>FuncAnimation()</code> function from Matplotlib&#8217;s powerful animation capabilities.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f30d.png" alt="🌍" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/matplotlib-animation/" data-type="post" data-id="8435" target="_blank" rel="noreferrer noopener">Matplotlib Animations &#8211; A Helpful Guide with Video</a> </p>



<p>Feel free to also watch our background explainer video on Matplotlib&#8217;s animation functionality &#8212; it&#8217;s a good investment in your education!</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Matplotlib Animation – A Helpful Illustrated Guide" width="937" height="703" src="https://www.youtube.com/embed/F57_0XPdhD8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Feel free to join our free <a href="https://blog.finxter.com/email-academy/" data-type="page" data-id="12278" target="_blank" rel="noreferrer noopener">email academy</a> to learn Python &#8212; we have cheat sheets too!</p>






<h2 class="wp-block-heading">Joke Conway&#8217;s Game of Life</h2>



<pre class="wp-block-preformatted"><code><strong>Q</strong>: What did the cells say after playing Conway's game of life?
<strong>A</strong>: Nothing, they didn't survive!</code></pre>
<p>The post <a href="https://blog.finxter.com/python-matplotlib-makes-conways-game-of-life-come-alive/">Python Matplotlib Makes Conway&#8217;s Game of Life Come Alive</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: blog.finxter.com @ 2026-04-19 17:28:46 by W3 Total Cache
-->