<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator> <link href="https://blog.jaysinh.dev/tags/python%20trick%20questions/feed.xml" rel="self" type="application/atom+xml"/> <link href="https://blog.jaysinh.dev/" rel="alternate" type="text/html"/> <updated>2026-08-01T12:49:14+00:00</updated> <id>https://blog.jaysinh.dev/</id> <title type="html">Jaysinh’s own heed</title> <subtitle>I am a Full-stack developer by profession, Computer scientist by heart and an Actor by gene. I write mostly on programming topics. Browse through my blog posts to identify my taste of writing. </subtitle> <entry> <title type="html">Analyzing the behaviour of Python function slice</title> <link href="https://blog.jaysinh.dev/2018/09/29/analyzing-the-behaviour-of-python-function-splice.html" rel="alternate" type="text/html" title="Analyzing the behaviour of Python function slice"/> <published>2018-09-29T04:47:46+00:00</published> <updated>2018-09-29T04:47:46+00:00</updated> <id>https://blog.jaysinh.dev/2018/09/29/analyzing-the-behaviour-of-python-function-splice</id> <content type="html" xml:base="https://blog.jaysinh.dev/2018/09/29/analyzing-the-behaviour-of-python-function-splice.html">&lt;p&gt;&lt;img src=&quot;/assets/images/python_slice_function/title_image.jpg&quot; alt=&quot;Title Image&quot; /&gt;&lt;/p&gt; &lt;p&gt;Last Friday, I was sitting in one of the good coffee shops in Bangalore with my friend. Coffee and discussion is the best combination to release stress. It was looking like a perfect Friday evening until my friend was struck by an idea of asking me a question.&lt;/p&gt; &lt;p&gt;“Let me conduct a quiz.” he said, interrupting our conversation.&lt;/p&gt; &lt;p&gt;“A quiz? Quiz on what?”, I asked.&lt;/p&gt; &lt;p&gt;“On programming”, he said&lt;/p&gt; &lt;p&gt;“What is the level of difficulty then?” I said.&lt;/p&gt; &lt;p&gt;Asking the level of difficulty is important. I never invest my efforts in solving something easy. If he had said easy, I would have ignored to answer, but he said “It is a bit difficult, but not that difficult. I gave a wrong answer to this question in my last interview.”&lt;/p&gt; &lt;p&gt;There was no reason to go back from here. Taking some deep breaths I said, “Please go ahead.”&lt;/p&gt; &lt;p&gt;He stood up, took a tissue paper from a nearby counter and scratched below code on it. &lt;sup id=&quot;fnref:1&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot; role=&quot;doc-noteref&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; And he asked me by pointing towards that code, “What will be the output of this code?”&lt;/p&gt; &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:])&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Now it was my turn to give the answer. I looked at the code and tried parsing it in my head — line by line. In my mind, I observed the first line. It was defining a function which seems to be correct. I moved my eyes to the next line. It was defining a variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt; of type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list&lt;/code&gt; and assigning values ranging from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;. Even this wasn’t looking problematic. So I forwarded to the next line where it was trying to print that variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt; by slicing it from starting value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;30&lt;/code&gt; to the infinity.&lt;/p&gt; &lt;p&gt;“Well, the start value is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;30&lt;/code&gt; which is greater than the length of the list. This should raise an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IndexError&lt;/code&gt;” I said in my mind. I was about to speak an answer, but suddenly Devil of me flashed.&lt;/p&gt; &lt;p&gt;“It is less than &lt;a href=&quot;http://catb.org/jargon/html/O/one-banana-problem.html&quot;&gt;a banana job&lt;/a&gt; my dear,” the Devil said to me, “You should take a little advantage of this opportunity my boy.”&lt;/p&gt; &lt;p&gt;Because things were looking in my control, I shook my hands with the Devil.&lt;/p&gt; &lt;p&gt;I said to my friend, “How about betting for some real values?”&lt;/p&gt; &lt;p&gt;Going closer I spoke, “If I answer correctly, You will pay the bill and If I am wrong, This will be a treat from my side.”&lt;/p&gt; &lt;p&gt;He thought for a while and nodded. Now it was my turn to unveil the cards.&lt;/p&gt; &lt;p&gt;I said in a strong voice, “It will raise an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IndexError&lt;/code&gt;.” And shifted my focus towards the chocolate.&lt;/p&gt; &lt;p&gt;He starred my face for a second and spoke, “Okay. Are you sure about this?”.&lt;/p&gt; &lt;p&gt;This was the hint he gave. I should have taken another shot here. What happens next become a lesson for me.&lt;/p&gt; &lt;p&gt;I said with a flat face, “Yes I am.”&lt;/p&gt; &lt;p&gt;With my answer, he instantly opened his backpack, took his Laptop out and typed the code which he wrote on that tissue.&lt;/p&gt; &lt;p&gt;When I stopped hearing a sound of typing I yelled, “So did I win?”&lt;/p&gt; &lt;p&gt;He turned his laptop towards me and exclaimed, “Not at all!”&lt;/p&gt; &lt;p&gt;When I focused on the screen, the interpreter was printing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt;. Damn! I lost the bet. Why the hell &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slice&lt;/code&gt; is returning an empty &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list&lt;/code&gt; even when we are trying to slice it with a value which is greater than the length of it! It was surely looking unpythonic behavior. I paid whatever the bill amount was. Entire evening this question was all roaming my mind. After coming home, I decided to justify reasons for returning an empty list instead of raising an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IndexError&lt;/code&gt; from a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slice&lt;/code&gt;.&lt;/p&gt; &lt;p&gt;Below are a few reasons justifying such behavior of slice function. I am sharing this with you so that you don’t lose a bet with your friend :) For those who haven’t used slice anytime in their life, I advise to read &lt;a href=&quot;https://docs.python.org/3.7/tutorial/introduction.html#lists&quot;&gt;this&lt;/a&gt; tutorial. Reading &lt;a href=&quot;https://docs.python.org/3.7/library/stdtypes.html#sequence-types-list-tuple-range&quot;&gt;this&lt;/a&gt; guide for understanding how a slice function converts the input values. Especially rule number 3 and 4 referenced there.&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;p&gt;&lt;strong&gt;Reason number one:&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Python lists are more commonly used in iterations. Consider below example:&lt;/p&gt; &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; &lt;/div&gt; &lt;p&gt;If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slice&lt;/code&gt; was raising an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IndexError&lt;/code&gt;, then the above code would have to written like this&lt;/p&gt; &lt;p&gt;written like like this&lt;/p&gt; &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;IndexError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; &lt;/div&gt; &lt;p&gt;Or in another way below is also looking reasonable&lt;/p&gt; &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; &lt;/div&gt; &lt;p&gt;Both the approaches are looking little lengthy by an obvious reason. And that reason is to prevent executing loop if there are no elements in it. When we observe the behavior of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slice&lt;/code&gt; called at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for in&lt;/code&gt;, it makes sense to return an empty list instead of raising an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IndexError&lt;/code&gt;.&lt;/p&gt; &lt;/li&gt; &lt;/ul&gt; &lt;p&gt;I am not able to find further reasons to return an empty list instead of raising the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IndexError&lt;/code&gt;. But I am sure, there will be. If you know any other potential reasons for such behavior of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slice&lt;/code&gt;, please drop me a mail at &lt;strong&gt;jaysinhp&lt;/strong&gt; at &lt;strong&gt;gmail&lt;/strong&gt; dot &lt;strong&gt;com&lt;/strong&gt; or contact me over Twitter &lt;a href=&quot;https://twitter.com/jaysinhp&quot;&gt;@jaysinhp&lt;/a&gt;. I will update the reasons at this post and give credits to you. Thanks for reading this post.&lt;/p&gt; &lt;h6 id=&quot;proofreaders-geoffrey-sneddon-elijah-mahendra-yadav-dhavan-vaidya&quot;&gt;Proofreaders: &lt;a href=&quot;https://github.com/gsnedders&quot;&gt;Geoffrey Sneddon&lt;/a&gt;, &lt;a href=&quot;https://mailto:thyarmageddon@gmail.com&quot;&gt;Elijah&lt;/a&gt;, &lt;a href=&quot;mailto:mahendra.k12@gmail.com&quot;&gt;Mahendra Yadav&lt;/a&gt;, &lt;a href=&quot;http://codingquark.com/&quot;&gt;Dhavan Vaidya&lt;/a&gt;,&lt;/h6&gt; &lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt; &lt;ol&gt; &lt;li id=&quot;fn:1&quot;&gt; &lt;p&gt;I am using the word “Scratch” because that tissue paper was such a thin that writing by a ballpen torn it. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt; &lt;/li&gt; &lt;/ol&gt; &lt;/div&gt;</content> <author> <name>Jaysinh Shukla</name> </author> <category term="python"/> <category term="slice"/> <category term="python trick questions"/> <summary type="html"></summary> </entry> </feed>