<story><title>Show HN: Refurb – A tool for refurbishing and modernizing Python codebases</title><url>https://github.com/dosisod/refurb</url></story><parent_chain><item><author>dosisod</author><text>Because tuples cannot change over time, they are (slightly) faster to create compared to lists. Like you mentioned, they are being created just to be iterated over. It is a small enough of a performance improvement that it more of a style choice then anything.&lt;p&gt;Bonus fact: You can use set iteration in for loops as well! This has the added benefit of sorting the values as well:&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; for x in {2, 1, 3}:
...  print(x)
1
2
3&lt;p&gt;You didn&amp;#x27;t ask for that, but I felt like sharing it, to there you go</text></item><item><author>asplake</author><text>To the example, why recommend replacing a list with a tuple when it is about to be iterated through? I’d choose a tuple if it was to be destructured, but not for this. I know it works either way, but this one feels off.</text></item></parent_chain><comment><author>minhazm</author><text>Sets are unordered. It doesn&amp;#x27;t even make logical sense when you think about it, it would make set insertion O(logn) which would absolutely wreck the performance. Your example just happens to accidentally work. Here&amp;#x27;s an example where it doesn&amp;#x27;t:&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; [i for i in {9,40,49}]
[40, 9, 49]</text></comment>
<story><title>Show HN: Refurb – A tool for refurbishing and modernizing Python codebases</title><url>https://github.com/dosisod/refurb</url></story><parent_chain><item><author>dosisod</author><text>Because tuples cannot change over time, they are (slightly) faster to create compared to lists. Like you mentioned, they are being created just to be iterated over. It is a small enough of a performance improvement that it more of a style choice then anything.&lt;p&gt;Bonus fact: You can use set iteration in for loops as well! This has the added benefit of sorting the values as well:&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; for x in {2, 1, 3}:
...  print(x)
1
2
3&lt;p&gt;You didn&amp;#x27;t ask for that, but I felt like sharing it, to there you go</text></item><item><author>asplake</author><text>To the example, why recommend replacing a list with a tuple when it is about to be iterated through? I’d choose a tuple if it was to be destructured, but not for this. I know it works either way, but this one feels off.</text></item></parent_chain><comment><author>evil-olive</author><text>that only works for small numbers. I suspect it&amp;#x27;s a side effect of the small integer cache CPython maintains [0]&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &amp;gt;&amp;gt;&amp;gt; for x in {2, 1, 3}: print(x)
    1
    2
    3
    &amp;gt;&amp;gt;&amp;gt; for x in {20, 10, 30}: print(x) 
    10
    20
    30
    &amp;gt;&amp;gt;&amp;gt; for x in {200, 100, 300}: print(x) 
    200
    100
    300
&lt;/code&gt;&lt;/pre&gt;
0: &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;15171695&amp;#x2F;whats-with-the-integer-cache-maintained-by-the-interpreter&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;15171695&amp;#x2F;whats-with-the-...&lt;/a&gt;</text></comment>