Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Join a List of Lists? You Don’t. You Flatten It!

#1
How to Join a List of Lists? You Don’t. You Flatten It!

Short answer: To flatten your list of list lst, use the nested list comprehension [x for l in lst for x in l] to iterate over all nested lists (for l in lst) and over all their elements (for x in l). Each element x, you just place in the outer list, unchanged.

When browsing StackOverflow, I stumble upon this question from time to time: “How to Join a List of Lists?“. My first thought is that the asking person is referring to the join() function that converts an iterable (such as a list) to a string by concatenating its elements.

But nothing can be further from the truth! The question is usually about “flattening” the list: to transform a nested list of lists such as [[1, 2, 3], [4, 5, 6]] to a flat list [1, 2, 3, 4, 5, 6]. So, the real question is:

How to Flatten a List of Lists in Python?


Problem: Given a list of lists. How to flatten the list of lists by getting rid of the inner lists—and keeping their elements?

Example: You want to transform a given list into a flat list like here:

lst = [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]] # ... Flatten the list here ... print(lst)
# [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]
Flatten a List of Lists with List Comprehension

Solution: Use a nested list comprehension statement [x for l in lst for x in l] to flatten the list.

lst = [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]] # ... Flatten the list here ...
lst = [x for l in lst for x in l] print(lst)
# [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]

Explanation: In the nested list comprehension statement [x for l in lst for x in l], you first iterate over all lists in the list of lists (for l in lst). Then, you iterate over all elements in the current list (for x in l). This element, you just place in the outer list, unchanged, by using it in the “expression” part of the list comprehension statement [x for l in lst for x in l].

Try It Yourself: You can execute this code snippet yourself in our interactive Python shell. Just click “Run” and test the output of this code.

Exercise: How to flatten a three-dimensional list (= a list of lists of lists)? Try it in the shell!

Video: List Comprehension Python List of Lists




Watch the video to learn three ways how to apply list comprehension to a list of lists:

  • to flatten a list of lists,
  • to create a list of lists, and
  • to iterate over a list of lists.

Well, if you really want to learn how to join() a list in Python, visit this detailed tutorial on the Finxter blog.

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2020/06/...latten-it/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python zip(): Get Elements from Multiple Lists xSicKxBot 0 1,973 08-26-2023, 01:28 AM
Last Post: xSicKxBot
  [Tut] Dictionary of Lists to DataFrame – Python Conversion xSicKxBot 0 1,380 04-17-2023, 03:46 AM
Last Post: xSicKxBot
  [Tut] How to Create a DataFrame From Lists? xSicKxBot 0 1,228 12-17-2022, 03:17 PM
Last Post: xSicKxBot
  [Tut] Easiest Way to Convert List of Hex Strings to List of Integers xSicKxBot 0 1,462 11-25-2022, 11:54 AM
Last Post: xSicKxBot
  [Tut] Python Find Shortest List in List xSicKxBot 0 1,374 09-25-2022, 03:42 AM
Last Post: xSicKxBot
  [Tut] Python Find Longest List in List xSicKxBot 0 1,304 09-23-2022, 02:19 PM
Last Post: xSicKxBot
  [Tut] How to Add Two Lists Element-wise in Python xSicKxBot 0 2,326 06-03-2022, 08:42 AM
Last Post: xSicKxBot
  [Tut] How to Convert List of Lists to Tuple of Tuples in Python? xSicKxBot 0 1,254 06-02-2022, 08:39 AM
Last Post: xSicKxBot
  [Tut] How to Convert Tuple of Tuples to List of Lists in Python? xSicKxBot 0 1,306 06-01-2022, 08:08 AM
Last Post: xSicKxBot
  [Tut] How to Create a Dictionary from two Lists xSicKxBot 0 1,207 04-27-2022, 05:37 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016