Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Concatenate two list of lists Row-wise-Python
Next article icon

How to Zip two lists of lists in Python?

Last Updated : 17 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

zip() function typically aggregates values from containers. However, there are cases where we need to merge multiple lists of lists. In this article, we will explore various efficient approaches to Zip two lists of lists in Python. List Comprehension provides a concise way to zip two lists of lists together, making the code more readable and often more efficient than using the zip() function with additional operations.

Example:

Python
l1 = [[1, 2], [3, 4], [5, 6]] l2=  [[7, 8], [9, 10], [11, 12]]   # Zipping list res = [(a, b) for a, b in zip(l1, l2)] print(res) 

Output
[([1, 2], [7, 8]), ([3, 4], [9, 10]), ([5, 6], [11, 12])] 

Explanation:

  • zip() function pairs corresponding sublists from l1 and l2.
  • Combines these pairs into a list of tuples.

Let’s explore more method to zip two list of list.

Table of Content

  • Using itertools.zip_longest
  • Using a Loop
  • Using numpy

Using itertools.zip_longest

This method allows us to zip two lists of different lengths, padding the shorter list with a specified default value. This ensures that both lists are iterated over completely, even if they have unequal lengths.

Example:

Python
import itertools a= [[1, 2], [3, 4]] b= [[5, 6], [7, 8], [9, 10]]  #Zipping with padding res = list(itertools.zip_longest(a, b, fillvalue=[])) print(res) 

Output
[([1, 2], [5, 6]), ([3, 4], [7, 8]), ([], [9, 10])] 

Explanation:

  • zip_longest() pairs sublists, filling missing values with [].
  • list() converts the pairs into a list of tuples.

Using a Loop

This method uses a loop to iterate through corresponding sublists in two lists and concatenate them with the + operator. The concatenated sublists are then added to a result list, which is printed at the end.

Example:

Python
a = [[1, 3], [4, 5], [5, 6]] b = [[7, 9], [3, 2], [3, 10]] res = []  # Iterating and concatenating sublists for i in range(len(a)):     res.append(a[i] + b[i]) print(res) 

Output
[[1, 3, 7, 9], [4, 5, 3, 2], [5, 6, 3, 10]] 

Explantion:

  • Combines corresponding sublists from a and b.
  • Appends the combined sublists to res.

Using numpy

When we are dealing with large datasets, numpy is a great choice for efficiency. It is specifically optimized for large-scale array operations and can perform zipping faster than standard Python lists when the data is numeric.

Example:

Python
import numpy as np l1 = [[1, 2], [3, 4], [5, 6]] l2 = [[7, 8], [9, 10], [11, 12]]  # Convert lists to numpy arrays res = np.array([np.array([a, b]) for a, b in zip(l1, l2)]) print(res) 

Output
[[[ 1  2]   [ 7  8]]   [[ 3  4]   [ 9 10]]   [[ 5  6]   [11 12]]] 

Explanation:

  • Pairs and converts sublists from l1 and l2 into NumPy arrays.
  • Combines them into a 2D NumPy array.


Next Article
Concatenate two list of lists Row-wise-Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Marketing
  • Python list-programs
  • python-list
Practice Tags :
  • python
  • python-list

Similar Reads

  • Concatenate two list of lists Row-wise-Python
    The task of concatenate two lists of lists row-wise, meaning we merge corresponding sublists into a single sublist. For example, given a = [[4, 3], [1, 2]] and b = [[7, 5], [9, 6]], we pair elements at the same index: [4, 3] from a is combined with [7, 5] from b, resulting in [4, 3, 7, 5], and [1, 2
    3 min read
  • How to iterate two lists in parallel - Python
    Whether the lists are of equal or different lengths, we can use several techniques such as using the zip() function, enumerate() with indexing, or list comprehensions to efficiently iterate through multiple lists at once. Using zip() to Iterate Two Lists in ParallelA simple approach to iterate over
    2 min read
  • Merge Two Lists into List of Tuples - Python
    The task of merging two lists into a list of tuples involves combining corresponding elements from both lists into paired tuples. For example, given two lists like a = [1, 2, 3] and b = ['a', 'b', 'c'], the goal is to merge them into a list of tuples, resulting in [(1, 'a'), (2, 'b'), (3, 'c')]. Usi
    3 min read
  • Convert Set of Tuples to a List of Lists in Python
    Sets and lists are two basic data structures in programming that have distinct uses. It is sometimes necessary to transform a collection of tuples into a list of lists. Each tuple is converted into a list throughout this procedure, and these lists are subsequently compiled into a single, bigger list
    3 min read
  • How to Split Lists in Python?
    Lists in Python are a powerful and versatile data structure. In many situations, we might need to split a list into smaller sublists for various operations such as processing data in chunks, grouping items or creating multiple lists from a single source. Let's explore different methods to split list
    3 min read
  • How to compare two lists in Python?
    In Python, there might be a situation where you might need to compare two lists which means checking if the lists are of the same length and if the elements of the lists are equal or not. Let us explore this with a simple example of comparing two lists. [GFGTABS] Python a = [1, 2, 3, 4, 5] b = [1, 2
    3 min read
  • Convert List of Tuples To Multiple Lists in Python
    When working with data in Python, it's common to encounter situations where we need to convert a list of tuples into separate lists. For example, if we have a list of tuples where each tuple represents a pair of related data points, we may want to split this into individual lists for easier processi
    4 min read
  • Python - Convert a list into tuple of lists
    When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists. For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this co
    3 min read
  • Python | Split nested list into two lists
    Given a nested 2D list, the task is to split the nested list into two lists such that the first list contains the first elements of each sublist and the second list contains the second element of each sublist. In this article, we will see how to split nested lists into two lists in Python. Python Sp
    5 min read
  • Python | Convert list of tuples to list of list
    Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples. Using numpyNumPy makes it easy to convert a list of t
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences