Wednesday, January 23, 2019

So a list and a tuple walk into a sum()

As a direct side effect of glom's 19.1.0 release, the authors here at PDW got to re-experience one of the more surprising behaviors of three of Python's most basic constructs:
Most experienced developers know the quickest way to combine a short list of short lists:
list_of_lists = [[1], [2], [3, 4]]
sum(list_of_lists, [])
# [1, 2, 3, 4]
Ah, nice and flat, much better.

But what happens when we throw a tuple into the mix:
list_of_seqs = [[1], [2], (3, 4)]
sum(list_of_seqs, [])
# TypeError: can only concatenate list (not "tuple") to list
This is kind of surprising! Especially when you consider this:
seq = [1, 2]
seq += (3, 4)
# [1, 2, 3, 4]
Why should sum() fail when addition succeeds?! We'll get to that.
new_list = [1, 2] + (3, 4)
# TypeError: can only concatenate list (not "tuple") to list
There's that error again!

The trick here is that Python has two addition operators. The simple "+" or "add" operator, used by sum(), and the more nuanced "+=" or "iadd" operator, add's inplace variant.

But why is ok for one addition to error and the other to succeed?

Symmetry. And maybe commutativity if you remember that math class.

"+" in Python is symmetric: A + B and B + A should always yield the same result. To do otherwise would be more surprising than any of the surprises above. list and tuple cannot be added with this operator because in a mixed-type situation, the return type would change based on ordering.

Meanwhile, "+=" is asymmetric. The left side of the statement determines the type of the return completely. A += B keeps A's type. A straightforward, Pythonic reason if there ever was one.

Going back to the start of our story, by building on operator.iadd, glom's new flatten() function avoids sum()'s error-raising behavior and works wonders on all manner of nesting iterable.

130 comments:

  1. Wow, this is new to me. Thanks for sharing it.

    ReplyDelete
  2. "+" is not very symmetric when you're adding strings... (or lists, for that matter, [1] + [2] != [2] + [1]).

    ReplyDelete
  3. >>> a=[4,6]
    >>> a += (i**2 for i in range(4))
    >>> a
    [4, 6, 0, 1, 4, 9]

    ReplyDelete
  4. A round of applause for your article.Much thanks again. Really Cool. Satta king

    ReplyDelete
  5. All of these posts were incredible perfect. It would be great if you’ll post more updates.
    Data Science Course in Bangalore

    ReplyDelete
  6. These thoughts just blew my mind. I am glad you have posted this.
    Data Science Training in Bangalore

    ReplyDelete
  7. Very awesome!!! When I seek for this I found this website at the top of all blogs in search engine. Oregon Business Registry

    ReplyDelete
  8. I have a mission that I’m just now working on, and I have been at the look out for such information. 토토사이트

    ReplyDelete
  9. Suppose if someone has bet 10 rupees on a number, then if that number is opened then the user will get 10 x 90 = 900 Rupees. Similarly, users will get 1800 rupees for 20 rupees, 2700 rupees for 30 rupees, 3600 rupees for 40 rupees and 4500 rupees for 50 rupees. The user can invest as much money as he wants on one number and can play many numbers as he wants. satta 786

    ReplyDelete
  10. Super site! I am Loving it!! Will return once more, I’m taking your food additionally, Thanks. 먹튀사이트

    ReplyDelete
  11. You can comment on the blog ordering system. You should discuss, it's splendid. Auditing your blog would increase the number of visitors. I was very happy to find this site. Thank you...

    Data Science Certification in Bangalore

    ReplyDelete
  12. I am overwhelmed by your post with such a beautiful subject. I usually visit their blogs and update myself through the information they contain, but today's blog would be most appreciated. Well done!
    Data Science Training in Pune

    ReplyDelete
  13. I am sure it will help many people. Keep up the good work. It's very compelling and I enjoyed browsing the entire blog.
    Data Science In Bangalore

    ReplyDelete
  14. From this post I know that your good knowledge of the game with all the pieces has been very helpful. I inform you that this is the first place where I find problems that I look for. You have a clever but attractive way of writing.
    Data Analytics Courses in Bangalore

    ReplyDelete
  15. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work. 토토사이트

    ReplyDelete
  16. One other issue is when you are in a predicament where you would not have a co-signer then you may genuinely wish to try to exhaust all of your financing options. You can get many grants or loans and other grants휴게텔

    that will give you finances to assist with school expenses. Thank you for the post.

    ReplyDelete
  17. Happy to chat on your blog, I feel like I can't wait to read more reliable posts and think we all want to thank many blog posts to share with us.

    Business Analytics Course

    ReplyDelete
  18. t's always a pleasure to read your magnificent articles on this site. You are among the top writers of this generation 토토

    ReplyDelete
  19. Instagram followers and I’m sure they’ll enjoy it as well 먹튀검증

    ReplyDelete
  20. Woohoo! It is an amazing and useful article. I really like. It's so good and so amazing. I am amazed. I hope you will continue to do your job in this way in the future as well.

    Digital Marketing Course in Bangalore

    ReplyDelete
  21. As I am looking at your writing, 온라인바둑이 I regret being unable to do outdoor activities due to Corona 19, and I miss my old daily life. If you also miss the daily life of those days, would you please visit my site once? My site is a site where I post about photos and daily life when I was free.
    Looking at this article, I miss the time when I didn't wear a mask. 온라인바둑이 Hopefully this corona will end soon. My blog is a blog that mainly posts pictures of daily life before Corona and landscapes at that time. If you want to remember that time again, please visit us.

    ReplyDelete
  22. Great website. Lots of useful info here. I am sending it to a few
    friends ans additionally sharing in delicious. 해외스포츠중계

    ReplyDelete
  23. I blog quite often and I genuinely appreciate your information. 바카라사이트

    ReplyDelete
  24. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts. 온라인홀덤
    Nice information, valuable and excellent design, as share good stuff with good ideas and concepts. 온라인홀덤

    ReplyDelete
  25. We heard of her uncle's return to Korea this summer.
    The bad weather kept me from taking a trip to Cheju.
    토토사이트

    ReplyDelete
  26. Actually when someone doesn't understand then its up to other viewers that they will assist, so here it takes place.

    Here is my web site - 휴게텔

    ReplyDelete
  27. This article was written by a real thinking writer. I agree many of the with the solid points made by the writer. I'll be back. 온라인카지노

    ReplyDelete
  28. the hub of Satta King sort of this sort and therefore the trade flourished during the 1980s and 1990s.for various Gali Result. gamer to return full circle the speciality of the games online for free of charge Desawar Result.
    The game of Satta King is one among the various sorts of gambling. It is, therefore, necessary for the players to understand about the Satta results which will be the idea of formulating winning strategies within the game of Satta Matka Gali Result. there are dynamic wins Desawar Result. Endeavour to make sure you set down a figure cost just a period of the mixture as of now picked up.the Satta King with help of our sport of Satta King play online.
    Satta result available on internet websites. to play Kalyan Satta Ghaziabaad Result.
    Ghaziabad Result
    Satta Matka Tips
    also Check Out :
    Satta King faridabad Result
    Ghaziabad Result

    ReplyDelete
  29. You are my inhalation, I have few web logs and very sporadically run out from to post .

    ReplyDelete
  30. A powerful share, I simply given this onto a colleague who was doing a little bit evaluation on this. And he in truth bought me breakfast because I discovered it for him.. smile. So let me reword that: Thanks for the deal with! However yeah Thanks for spending the time to debate this, I really feel strongly about it and love studying more on this topic. If doable, as you turn into experience, would you thoughts updating your blog with extra particulars? It’s extremely useful for me. Big thumb up for this weblog post 립카페!

    ReplyDelete
  31. Looking forward to reading more. Great article post. Fantastic. Thanks so much for the blog. Much obliged 타이마사지.

    ReplyDelete
  32. Hi there! I’m at work browsing your blog from my new iphone 3gs! Just wanted to say I love reading through your blog and look forward to all your posts! Carry on the fantastic work 안마!

    ReplyDelete
  33. You're so interesting! I don't suppose I've read anything like this before.
    So great to find someone with a few unique thoughts on this issue.
    Seriously.. many thanks for starting this up. This site is something that is needed on
    the internet, someone with some originality!안마

    ReplyDelete
  34. Attractive section of content. I simply stumbled upon your blog and in accession capital to assert that I acquire in fact enjoyed
    account your blog posts. Anyway I’ll be subscribing
    on your augment or even I success you access constantly quickl

    Also visit my site:
    egaon casino

    ReplyDelete
  35. kinda amazing and thankful finding it, so wonderful thanks to the writer .
    토토사이트

    ReplyDelete
  36. what a nice article i found , i was looking it for a long time , i been searching it for so long now that i found out really help me.
    카지노사이트

    ReplyDelete
  37. Hi there mates, how is all, and what you would like to say concerning this
    article, in my view its truly awesome in favor of me. 안마


    ReplyDelete
  38. I am so grateful for your post.Much thanks again. Really Great.

    야동

    ReplyDelete
  39. “He likes to take a traditional and risk-averse approach to things over a creative one.”

    외국인출장

    ReplyDelete
  40. This comment has been removed by the author.

    ReplyDelete
  41. It's a fantastic write-up. Where did you see this handwriting? You are a fantastic individual, and also I desire you to provide me some recommendations on just how to compose such as this. I desire you to have a look at what I composed as well as offer me some guidance on what you assume, and also I'll be awaiting your recommendations. 카지노사이트

    ReplyDelete
  42. Generally I do not read article on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been amazed me. Thanks, very nice article.
    바카라

    ReplyDelete
  43. Hi, i think that i saw you visited my weblog thus i came to “return the favor”.I am attempting to find things to enhance my site!I suppose its ok to use some of your ideas!!
    바카라사이트

    ReplyDelete



  44. Hey I'm at work surfing around your blog from my new iphone 3gs! Just wanted to say I
    love reading through your blog and look forward to all your posts! Carry on the superb work!

    Review my page please: 휴게텔
    (jk)


    ReplyDelete
  45. Some truly grand work on behalf of the owner of this website , dead outstanding written content.

    파워볼

    ReplyDelete
  46. You are incredible! Thank you! . I'll be coming back to your site for more soon.온라인바둑이
    ‮‬

    ReplyDelete
  47. I have a lot of spray lining knowledge but always learn something new. Keep up the good work and thank you again.카지노사이트

    ReplyDelete
  48. Excellent post. I was checking continuously this blog and I’m impressed! Very helpful information particularly the last part :) I care for such information much. I was seeking this particular info for a very long time. Thank you and best of luck. ยูฟ่า168

    ReplyDelete
  49. I wanted to leave a little comment to support you and wish you the best of luck. We wish you the best of luck in all of your blogging endeavors.
    Digital Marketing Course in Bangalore

    ReplyDelete
  50. Been looking for this article for long time ago and finally found here. Thanks for the tips and sharing this post. appreciate!Please check out my website too and let me know what you think.
    온라인카지노

    ReplyDelete
  51. Hi to every one, since I am truly keen of reading this webpage’s post to be updated on a regular basis.
    It consists of pleasant stuff.

    my web page; 대구오피

    (freaky)

    ReplyDelete
  52. Hey! I'd like to start a blog so I can easily share my own experience and feelings online. Please let me know if you have any kind of ideas or tips for new
    aspiring blog owners. 토토사이트

    ReplyDelete

  53. Hi to every one, since I am truly keen of reading this webpage’s post to be updated on a regular basis.
    It consists of pleasant stuff.

    my web page;경마

    ReplyDelete
  54. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers!

    오피헌터

    ReplyDelete
  55. "Unquestionably believe that which you said. Your favorite reason seemed to be on the net
    the simplest thing to be aware of. I say to you, I certainly get annoyed while people consider worries that they plainly do not know about.
    You managed to hit the nail upon the top and also defined out
    the whole thing without having side-effects , people could take a signal.

    Will likely be back to get more. Thanks 스포츠마사지"

    ReplyDelete
  56. whoah this blog is wonderful i really like studying your articles.
    Keep up the good work! You already know, many persons are searching around for
    this information, you can aid them greatly.
    오피월드

    ReplyDelete
  57. If some one wishes to be updated with newest technologies after that he must be pay a quick visit this site and be up to date all the time. 릴게임

    ReplyDelete
  58. Pretty! This was a really wonderful post. Thank you for your provided information. 토토


    ReplyDelete
  59. this site would have more fun and interesting click this to figure out.
    오피월드


    ReplyDelete
  60. I stumbled onto your blog and read a few post.I m very pleased to read this article.카지노사이트

    ReplyDelete
  61. Just how can you have such abilities? I can not evaluate your abilities yet, yet your writing is fantastic. I considered my instructions once more. I desire a professional like you to review my writing and also court my writing since I'm actually interested concerning my abilities. just click the following website

    ReplyDelete
  62. I was very pleased to find this page. I wanted to thank you for your time due to this wonderful read!! I definitely loved every part of it and I have you bookmarked to check out new information on your blog. Feel free to visit my website; 온라인카지노사이트

    ReplyDelete
  63. Everything is very open with a precise clarification of the issues. It was really informative. Your site is very helpful. Thanks for sharing! Feel free to visit my website; 카지노사이트링크

    ReplyDelete
  64. Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job Feel free to visit my website; 온라인카지노사이트넷

    ReplyDelete
  65. Great job. Thank you so much for sharing your wonderful site! Kindly visit my website안전놀이터

    ReplyDelete


  66. Wonderful article, thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here. 경마사이트

    ReplyDelete
  67. will be praised anywhere. I am a columnist and I am writing articles related of your site. You can also check my article 토토사이트검증

    ReplyDelete
  68. I appreciate your information in this article. It’s smart, well-written and easy to understand. You have my attention on this subject.안전한놀이터 I will be back

    ReplyDelete
  69. my day because you never know how much you make my day happier and more complete.
    There are even times when I feel so down but I will feel better right after checking your blogs 메이저검증업체

    ReplyDelete
  70. dependably manufacture astonishing entities We are linking to this great post on our website 먹튀폴리스

    ReplyDelete
  71. i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future 스포츠토토

    ReplyDelete
  72. I can't believe there's a post like this You’re incredible! Thank you! 메이저놀이터

    ReplyDelete
  73. It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.

    Business Analytics Course

    ReplyDelete
  74. I appreciate your information 사설토토사이트 in this article. It’s smart, well-written and easy to understand. You have my attention on this subject

    ReplyDelete
  75. 안전공원추천 more interest in some of them hope you will give more information on this topics in your next articles

    ReplyDelete
  76. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience 토토검증업체

    ReplyDelete
  77. I think this good information can only be found here. I will always visit this blog. Thank you for the good comments. 먹튀보증업체

    ReplyDelete
  78. There are a lot of writings, but I think it s so good to have some information that I really need from time to time. 메이저안전사이트

    ReplyDelete
  79. WOW just what I was looking for. Came here by searching
    for online gaming sites 스포츠토토


    ReplyDelete
  80. You came up with this blog very creatively. The way it is written with such perfect language is really impressive. The content is well-written.
    Stainless Steel pipes and fittings

    ReplyDelete
  81. Are you looking for Satta king results here? So here you can see the result of Jodi Satta King in time.Desawar Satta, Faridabad Satta, Gaziabad Satta, and Gali Satta play very prominent roles.

    Satta king

    Satta king don Satta king badshah

    Satta king Guising forum

    ReplyDelete
  82. Greetings! Very useful advice within this article! It’s the little changes that make the most significant changes. Thanks a lot for sharing!경마

    ReplyDelete
  83. When I read your article on this topic, the first thought seems profound and difficult. There is also a bulletin board for discussion of articles and photos similar to this topic on my site, but I would like to visit once when I have time to discuss this topic. sòng bạc


    ReplyDelete
  84. I accidentally searched and visited your site. I still saw several posts during my visit, but the text was neat and readable. I will quote this post and post it on my blog. Would you like to visit my blog later? 메가슬롯


    ReplyDelete
  85. I'm so happy to finally find a post with what I want. 메리트카지노 You have inspired me a lot. If you are satisfied, please visit my website and leave your feedback.


    ReplyDelete
  86. Many thanks for the article, I have a lot of spray lining knowledge but always learn something new. Keep up the good work and thank you again. 온라인바카라


    ReplyDelete
  87. Thank you for sharing your knowledge. I really admire your efforts and I will be waiting for your next post thank you once again. Name Plate manufacturer in Jaipur

    ReplyDelete
  88. From some point on, I am preparing to build my site while browsing various sites. It is now somewhat completed. If you are interested, please come to play with 토토사이트!!


    ReplyDelete
  89. Your ideas inspired me very much. 크레이지슬롯 It's amazing. I want to learn your writing skills. In fact, I also have a website. If you are okay, please visit once and leave your opinion. Thank you.


    ReplyDelete
  90. Hello ! I am the one who writes posts on these topics메가슬롯 I would like to write an article based on your article. When can I ask for a review?


    ReplyDelete
  91. When I read an article on this topic, 우리카지노 the first thought was profound and difficult, and I wondered if others could understand.. My site has a discussion board for articles and photos similar to this topic. Could you please visit me when you have time to discuss this topic?


    ReplyDelete
  92. I was impressed by your writing. Your writing is impressive. I want to write like you.파워볼사이트 I hope you can read my post and let me know what to modify. My writing is in I would like you to visit my blog.


    ReplyDelete
  93. This is a very impressive subject. Thank you for always. I have been reading your article interestingly. If possible, please visit my website to read my posts and leave comments. Have a nice day! 바카라사이트 What you wrote was very helpful to me. Thank you. Actually, I run a site similar to you. If you have time, could you visit my site? Please leave your comments after reading what I wrote. If you do so, I will actively reflect your opinion. I think it will be a great help to run my site. Have a good day.


    ReplyDelete
  94. Of course, your article is good enough, 카지노사이트 but I thought it would be much better to see professional photos and videos together. There are articles and photos on these topics on my homepage, so please visit and share your opinions.


    ReplyDelete
  95. It's really great. Thank you for providing a quality article. There is something you might be interested in. Do you know 룰렛사이트? If you have more questions, please come to my site and check it out!


    ReplyDelete
  96. Don't go past my writing! Please read my article only once. Come here and read it once 카지노사이트


    ReplyDelete
  97. Nice to meet you. Your website is full of really interesting topics. It helps me a lot. I have a similar site. We would appreciate it if you visit once and leave your opinion. 카지노게임


    ReplyDelete
  98. This is the perfect post.슬롯사이트 It helped me a lot. If you have time, I hope you come to my site and share your opinions. Have a nice day.


    ReplyDelete
  99. Your post has really helped me a lot. I live in a different country than you, but I believe it will help a lot in my country. 카지노사이트 Thank you very much. Can I refer to your post on my website? Your post touched me a lot and helped me a lot. If you have any questions, please visit my site and read what kind of posts I am posting. I am sure it will be interesting.


    ReplyDelete
  100. Attractive element of content. I simply stumbled upon your site and
    in accession capital to say that I acquire in fact enjoyed account your blog posts.
    Any way I’ll be subscribing on your feeds or even I success
    you get entry to persistently fast.


    카지노사이트
    바카라사이트
    카지노사이트추천

    ReplyDelete
  101. Very nice article, I enjoyed reading your post, very nice share, I want to twit this to my followers. Thanks!. 바카라사이트

    ReplyDelete
  102. While looking for articles on these topics, I came across this article on the site here. As I read your article, I felt like an expert in this field. I have several articles on these topics posted on my site. Could you please visit my homepage? Nhà cái lừa đảo


    ReplyDelete
  103. From some point on, I am preparing to build my site while browsing various sites. It is now somewhat completed. If you are interested, please come to play with 바카라사이트!!


    ReplyDelete
  104. After going over a few of the blog posts on your web page,
    I honestly like your way of writing a blog. I book-marked it to my bookmark webpage list and will be checking back soon. Take a look at my
    web site too and let me know how you feel.



    온라인카지노사이트
    온라인카지노
    카지노사이트

    ReplyDelete
  105. I've been looking for info on this topic for a while. I'm happy this one is so great. Keep up the excellent work 에볼루션카지노

    ReplyDelete
  106. I also benefit from learning the assessments, but learn that alot of people ought to stay on essay to try and add worth in the direction of the authentic weblog release. 먹튀검증

    ReplyDelete
  107. Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards homestay melaka

    ReplyDelete
  108. That’s the reason concentrate on you have to particular footwork ahead of when writing. Is going to be feasible in order to much more appealing article in this way 먹튀검증

    ReplyDelete
  109. Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work 먹튀검증사이트

    ReplyDelete
  110. I want looking at and I believe this website got some really useful stuff on it! canaan avalon 1166 pro

    ReplyDelete
  111. Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work rat pest control london

    ReplyDelete
  112. This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article freight shipping company

    ReplyDelete
  113. Thank you for any other informative site. The place else could I am getting that kind of information written in such a perfect approach? I have a mission that I'm just now running on, and I have been on the look out for such info Digital marketing sydney

    ReplyDelete
  114. Great job here on. I read a lot of blog posts, but I never heard a topic like this. I Love this topic you made about the blogger's bucket list. Very resourceful Shipping To FBA From China

    ReplyDelete
  115. This type of message always inspiring and I prefer to read quality content, so happy to find good place to many here in the post, the writing is just great, thanks for the post. french provincial furniture

    ReplyDelete
  116. Nice post love it check my site for fast Satta King we provide superfast and all time result Sattaking

    ReplyDelete
  117. Thanks for another fantastic post. The place else could anybody get that kind of info in such a perfect method of writing? I have a presentation next week, and I’m at the search for such information. ufabet99

    ReplyDelete
  118. I gave up on Python a few years ago because it is difficult for me to comprehend. It's a huge challenge to beginners in programming just like me. Greetings from Piano Movers Edmonton

    ReplyDelete
  119. Paragraph writing is also a fun, if you be acquainted with then you can write or else it is complicated to write. แอพสล็อต ได้เงินจริง

    ReplyDelete
  120. Your Content is amazing and I am glad to read them. Thanks for sharing the Blog.this blog is very helpful information for every one.

    best seo expert in pakistan
    kaepernick net worth
    cabecero etnico

    ReplyDelete
  121. Python's a real headache. I am adept in programming and using Python really brings a lot of stress. Greetings from us at paintersgrandeprairie.com

    ReplyDelete
  122. I truly appreciate essentially perusing the entirety of your weblogs. Basically needed to advise you that you have individuals like me who value your work. Certainly an extraordinary post. Caps off to you! The data that you have given is useful. data science course in Varanasi

    ReplyDelete
  123. Create BacklinksI am really enjoying reading your well written articles. I think you spend numerous effort and time updating your blog.

    ReplyDelete
  124. Sach A Informative Website. Daily Updated Info. Really Like It And Bookmarked It. Visit Daily To Keep Me Update.
    Mass Backlinks

    ReplyDelete
  125. The game starting points with the individual exciting in a strange spot, and he should get himself out. He a little while later fathoms that he is for Pandora's Circumstance, and he should figure out a workable method for moving endlessly. Check for more pandora box arcade

    ReplyDelete