Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7958463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:25:36+00:00 2026-06-04T04:25:36+00:00

For lack of a better name, I’d like to do an izip_sorted in Python.

  • 0

For lack of a better name, I’d like to do an “izip_sorted” in Python. The input to the function is a number of iterables, each sorted. The ouput is a single iterable with sorted output.

print([x for x in izip_sorted([0,4,8], [1,3,5], [12,12,42],[])])

Edit: This is a simple example. The real usage will be on about 40 input iterables, each with about 100000 elements. Each element is a class stores a dict and implements __cmp__ so that the elements can be sorted. The data is too large to read in all at once.

should print

[0, 1, 3, 4, 5, 8, 12, 12, 42]

I have a solution but I’m new to python and I don’t know that it’s very Pythonic. Can this be improved upon? The sort where only 1 element has changed seems wasteful…

def izip_sorted(*iterables):
    """
    Return an iterator that outputs the values from the iterables, in sort order
    izip_sort('ABF', 'D', 'CE') --> A B C D E F
    """
    iterators = [iter(it) for it in iterables]
    current_iterators = []
    for it in iterators:
        try:
            current_iterators.append((next(it), it))
        except StopIteration:
            pass
    current_iterators.sort(key=lambda x: x[0])
    while(current_iterators):
        yield current_iterators[0][0]
        try:
            current_iterators[0] = (next(current_iterators[0][1]), current_iterators[0][1])
            current_iterators.sort(key=lambda x: x[0])
        except StopIteration:
            current_iterators = current_iterators[1:]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:25:37+00:00Added an answer on June 4, 2026 at 4:25 am

    If the inputs are not sorted, then they must all be realized (essentially, turned from an iterable into a list). You can’t sort without looking at the data. LattyWare’s solution is the most pythonic.

    If on the other hand, the input iterables are known to be sorted, you can use heapq.merge:

    >>> from heapq import merge
    >>> merge(*iterables)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm extending Numerics with a method I call Boundarize for lack of better name;
I have a small issue(for lack of a better word) with MySQL db. I
I have a list of paths (for lack of a better word, maybe bread
I am planning to build, for lack of a better term, a multi user
I have some data that isn't properly partitioned (for lack of a better word).
From what I gather, AppEngine fires up Application Instances (for a lack of better
Is there support in Ruby for (for lack of a better word) non-escaped (verbatim)
Suppose (for lack of better example) I have a model Person and another model
I´m having, in lack of better words, a hell of a time finding out
This seems so basic, I'm flabbergasted for lack of a better word. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.