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

  • Home
  • SEARCH
  • 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 7879825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:59:32+00:00 2026-06-03T03:59:32+00:00

Here’s what I have so far: def is_ordered(collection): if isinstance(collection, set): return False if

  • 0

Here’s what I have so far:

def is_ordered(collection):
    if isinstance(collection, set):
        return False
    if isinstance(collection, list):
        return True
    if isinstance(collection, dict):
        return False

    raise Exception("unknown collection")

Is there a much better way to do this?

NB: I do mean ordered and not sorted.

Motivation:

I want to iterate over an ordered collection. e.g.

def most_important(priorities):
    for p in priorities:
        print p

In this case the fact that priorities is ordered is important. What kind of collection it is is not. I’m trying to live duck-typing here. I have frequently been dissuaded by from type checking by Pythonistas.

  • 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-03T03:59:32+00:00Added an answer on June 3, 2026 at 3:59 am

    If the collection is truly arbitrary (meaning it can be of any class whatsoever), then the answer has to be no.

    Basically, there are two possible approaches:

    1. know about every possible class that can be presented to your method, and whether it’s ordered;
    2. test the collection yourself by inserting into it every possible combination of keys, and seeing whether the ordering is preserved.

    The latter is clearly infeasible. The former is along the lines of what you already have, except that you have to know about every derived class such as collections.OrderedDict; checking for dict is not enough.

    Frankly, I think the whole is_ordered check is a can of worms. Why do you want to do this anyway?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem...I have a page that loads a list of clients and
Here's my situation: I have a Data Template set up which contains a ToggleButton
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY
Here is the situation, I am attempting to fire a set of Gallio tests
Here is an example: I have a file 1.js, which has some functions. I
Here is my situation: I am using telerik with winform. I have a dataset
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here is an example: I have the generic type called Account. I wish to
Here I have an int x=3; NSLog(@%i, x); How to have it displayed like

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.