• Python Snacks
  • Posts
  • Flattening lists with this unconventional method

Flattening lists with this unconventional method

Python jobs, packages, tutorials, articles, and more.

😄 Hey there!

Happy new year! No “new year, new me” here. We’re full steam ahead with this newsletter (and my other newsletter, Bytes and Brew).

Here’s what’s in store for today:

  • A small update to the Python Pantry,

  • Using Python’s built-in sum() function to flatten lists,

  • And much more…

Forwarded this email? Sign up here for free.

🥐 The Python Pantry

Curated links to help you become a better programmer

🎒 Tutorials
- How To Build a Comprehensive Flask REST API in 5 Minutes using Flask-Musk (link)
- A Comprehensive Guide to YAML Using Python (link)

📦️ Packages
- Microdot: a lightweight flask-like web framework (link)
- attrs: Replacement for dunder methods in class definitions (link)

💰️ Job Postings
- Principle Software Engineer. Remote, KS, USA (link)
- Junior Python Developer. Texas, USA (link)

📕 Articles
- Python Gotcha - List Copy Problems (link)
- Hide Those Terminal Secrets using Warp (link)

In case you weren’t aware, I write another newsletter about artificial intelligence. Python is a language that is used to create a lot of the models that are used today.

Subscribe today for free to learn more about how you can use AI right here, right now.

Bytes and BrewLearn how you can use AI to craft solutions to solve real-world problems

💡 This Week’s Snack & Challenge

The weekly tip/trick and coding challenge

There’s different ways to flatten lists, from using itertools, numpy, and list comprehension.

But one thing I found very interesting is using the built-in sum function to flatten lists.

Yes, you read that right. Here’s a simple example:

>>> x = [[1, 2, 3], [4, 5]]
>>> x
[[1, 2, 3], [4, 5]]
>>> sum(x, [])
[1, 2, 3, 4, 5]

Here’s why this works:

  1. sum takes 2 arguments: your iterable and a start value. Start value defaults to 0.

  2. On the first iteration, it takes [] + [1, 2, 3], which results in [1, 2, 3].

  3. On the second iteration, it joins the 2 lists together: [1, 2, 3] + [4, 5], resulting in [1, 2, 3, 4, 5]

While this is a really nifty trick, there’s a few reasons why I wouldn’t use this in any production code:

  1. There’s more efficient ways (such as itertools),

  2. It’s not clear as to what the code is doing since it’s an unconventional way of expressing list-flattening,

  3. You can’t have any other data type except lists as your elements.

For example, sum([[1, 2], 3, [4, 5]], []) won’t work since the second element is an integer, not a list; you can’t add a list to an integer.

Here’s a few alternatives to flatten lists that are more conventional:

  1. List comprehension

  2. itertools.chain.from_iterable()

  3. numpy.flatten()

  4. functools.reduce()

  5. A simple nested loop 😃 

With this information, give this week’s challenge a shot:

Flatten the following list using 3 different ways:

[[1, 2, [3, 4]], [5, 6], 7]

The final list should be:

[1, 2, 3, 4, 5, 6, 7]

📋️ How I Can Help

Looking to accelerate your Python skills? Here’s a way you can do so:

  1. Hire me as a Python coach. Get in touch today!

  2. Book a 30 minute 1-on-1 with me to talk Python or AI.

  3. Refer your friend to this newsletter and get a Python code optimization guide after 1 referral. See below for your unique link.

Did you enjoy this week's snack?

Login or Subscribe to participate in polls.

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.