- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Ways to iterate tuple list of lists
List is an important container and used almost in every code of day-day programming as well as web-development, more it is used, more is the requirement to master it and hence knowledge of its operations is necessary.
Example
# using itertools.ziplongest # import library from itertools import zip_longest # initialising listoflist test_list = [ [('11'), ('12'), ('13')], [('21'), ('22'), ('23')], [('31'), ('32'), ('33')] ] # printing intial list print ("Initial List = ", test_list) # iterate list tuples list of list into single list res_list = [item for my_list in zip_longest(*test_list) for item in my_list if item] # print final List print ("Resultant List = ", res_list) # using itertools.ziplongest + lambda + chain # import library from itertools import zip_longest, chain # initialising listoflist test_list = [ [('11'), ('12'), ('13')], [('21'), ('22'), ('23')], [('31'), ('32'), ('33')] ] # printing intial list print ("Initial List = ", test_list) # iterate list tuples list of list into single list # using lambda + chain + filter res_list = list(filter(lambda x: x, chain(*zip_longest(*test_list)))) # print final List print ("Resultant List = ", res_list) # list using list comprehension # initialising listoflist test_list = [ [('11'), ('12'), ('13')], [('21'), ('22'), ('23')], [('31'), ('32'), ('33')] ] # printing intial list print ("Initial List = ", test_list) # iterate list tuples list of list into single list # using list comprehension res_list = [item for list2 in test_list for item in list2] # print final List print ("Resultant List = ", res_list)
- Related Articles
- Convert a list into tuple of lists in Python
- Unpacking tuple of lists in Python
- Python program to iterate over multiple lists simultaneously?
- Python - Ways to create a dictionary of Lists
- How to iterate through a tuple in Python?
- Python to Find number of lists in a tuple
- Flatten tuple of List to tuple in Python
- Python - Convert List of lists to List of Sets
- Sort lists in tuple in Python
- How to join list of lists in python?
- Convert list into list of lists in Python
- How to iterate over a C# tuple?
- Dictionary to list of tuple conversion in Python
- How to iterate through a list in Python?
- Iterate through Java Unit Tuple

Advertisements