How to use a list comprehension on a list of lists in Python

List comprehensions provide a concise way to create new lists by performing operations on each member of another sequence. Using a list comprehension on a list of lists performs an operation on each nested element and returns a new list of lists.

Solution for How to use a list comprehension on a list of lists in Python : You can use a nested list comprehension on a list of lists Use the syntax [[expression for j in i] for i in list_of_lists] to loop through every list i in list_of_lists and execute expression for every element j within i. This returns a new list of lists.


how-to-use-a-list-comprehension-on-a-list-of-lists-in-python