<?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>lists Archives - PythonRight</title>
	<atom:link href="https://pythonright.com/tag/lists/feed/" rel="self" type="application/rss+xml" />
	<link>https://pythonright.com/tag/lists/</link>
	<description>Clean code is written by those who care.</description>
	<lastBuildDate>Fri, 07 Dec 2018 10:30:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.2</generator>

<image>
	<url>https://pythonright.com/wp-content/uploads/2018/11/python-right-logo-150x150.png</url>
	<title>lists Archives - PythonRight</title>
	<link>https://pythonright.com/tag/lists/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>What&#8217;s the difference between list() and []</title>
		<link>https://pythonright.com/whats-the-difference-between-list-function-and-literal-syntax/</link>
					<comments>https://pythonright.com/whats-the-difference-between-list-function-and-literal-syntax/#comments</comments>
		
		<dc:creator><![CDATA[Julian Camilleri]]></dc:creator>
		<pubDate>Wed, 05 Dec 2018 12:22:06 +0000</pubDate>
				<category><![CDATA[Python 3]]></category>
		<category><![CDATA[lists]]></category>
		<guid isPermaLink="false">http://www.pythonright.com/?p=68</guid>

					<description><![CDATA[<p>What are the key differences between using list() and []? The most obvious and visible key difference between [python]list()[/python] and [python][][/python] is the syntax. Putting the syntax aside for a minute here, someone whose new or intermediately exposed to python might argue that they&#8217;re both lists or derive from the same class; that is true. &#8230; <a href="https://pythonright.com/whats-the-difference-between-list-function-and-literal-syntax/" class="more-link">Continue reading <span class="screen-reader-text">What&#8217;s the difference between list() and []</span></a></p>
<p>The post <a rel="nofollow" href="https://pythonright.com/whats-the-difference-between-list-function-and-literal-syntax/">What&#8217;s the difference between list() and []</a> appeared first on <a rel="nofollow" href="https://pythonright.com">PythonRight</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="schema-faq wp-block-yoast-faq-block"><div class="schema-faq-section"><strong class="schema-faq-question"><h1>What are the key differences between using list() and []?</h1></strong> <p align="justify" class="schema-faq-answer">The most obvious and visible key difference between [python]list()[/python] and [python][][/python] is the syntax. Putting the syntax aside for a minute here, someone whose new or intermediately exposed to python might argue that they&#8217;re both lists or derive from the same class; that is true. Which furthermore increases the importance of understanding the key differences of both, most of which are outlined below.<br><br>[python]list()[/python] is a <em>function</em> and [python][][/python] is <em><a href="#whats-
a-literal">literal syntax</a></em>.<br>
<div style="display: inline-block; max-width: 100%; overflow: hidden;">
<div class="wp-block-image" style="display: inline-block;"><figure><img decoding="async" fetchpriority="high" src="http://www.pythonright.com/wp-content/uploads/2018/12/with-literals.png" width="400" height="220" alt="Literal syntax" class="wp-image-94" style="max-height: 220px;"/><figcaption>Literal Syntax &#8211; <em>src:excess.org</em></figcaption></figure></div>
<div class="wp-block-image" style="float: left; display: inline-block;"><figure class="alignleft"><img decoding="async" src="http://www.pythonright.com/wp-content/uploads/2018/12/without-literals.png" alt="Function" width="400" height="220" class="wp-image-95" style="max-height: 220px;" /><figcaption>Function &#8211; <em>src:excess.org</em></figcaption></figure></div>
</div>

<br>Let&#8217;s take a look at what happens when we call [python]list()[/python] and [python][][/python] respectively through the <a href="https://docs.python.org/3/library/dis.html" target="_blank" title="Python docs for disassembler">disassembler</a>.</p>
[python]>>> import dis
>>> print(dis.dis(lambda: list()))
  1           0 LOAD_GLOBAL              0 (list)
              3 CALL_FUNCTION            0 (0 positional, 0 keyword pair)
              6 RETURN_VALUE
None
>>> print(dis.dis(lambda: []))
  1           0 BUILD_LIST               0
              3 RETURN_VALUE
None[/python]
The output from the disassembler above shows that the literal syntax version doesn&#8217;t require a global lookup, denoted by the op code <a href="https://docs.python.org/3/library/dis.html#opcode-LOAD_GLOBAL" target="_blank">LOAD_GLOBAL</a> or a function call, denoted by the op code <a href="https://docs.python.org/3/library/dis.html#opcode-CALL_FUNCTION" target="_blank">CALL_FUNCTION</a>. 
</br></br>
As a result, literal syntax is faster than it&#8217;s counterpart. &#8211; Let&#8217;s take a second and look at the timings below.
</br></br>
[python]import timeit
>>> timeit.timeit(&#8216;[]&#8217;, number=10**4)
0.0014592369552701712
>>> timeit.timeit(&#8216;list()&#8217;, number=10**4)
0.0033833282068371773[/python]   

On another note it&#8217;s equally important and worth pointing out that literal syntax, [python][][/python] <b>does not</b> unpack values. 

An example of unpacking is shown below.</br></br>
[python]
>>> list(&#8216;abc&#8217;) # unpacks value
[&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]
>>> [&#8216;abc&#8217;] # value remains packed
[&#8216;abc&#8217;]
[/python]</p></div> <div class="schema-faq-section"><strong class="schema-faq-question">What&#8217;s a literal in python?</strong> <a class="anchor-link" name="whats-a-literal" href='#whats-a-literal' title="Permalink to this headline">&para;</a> <p align="justify" class="schema-faq-answer">Literals are notations or a way of writing constant or raw variable values which python recognises as built-in types.</p> </div> </div>
<hr>
<p align="justify">It has been fun and interesting to write the first of many to come <a href="/">PythonRight</a> blog posts; in the next blog post we&#8217;ll be going over the beauty of unpacking , so stay tuned. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> &#8211; If you have any feedback or any other topics that you&#8217;d like to see explained in detail, do feel free to comment.</p></div>



<p></p>
<p>The post <a rel="nofollow" href="https://pythonright.com/whats-the-difference-between-list-function-and-literal-syntax/">What&#8217;s the difference between list() and []</a> appeared first on <a rel="nofollow" href="https://pythonright.com">PythonRight</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://pythonright.com/whats-the-difference-between-list-function-and-literal-syntax/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
