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 6561885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:38:34+00:00 2026-05-25T13:38:34+00:00

I want a dict or tuple I can sort based on attributes of the

  • 0

I want a dict or tuple I can sort based on attributes of the objects I’m using as arguments for *arg. The way I’ve been trying to do it just gives me AttributeErrors, which leads me to believe I’m doing it weird.

def function(*arg):
    items = {}
    for thing in arg:
        items.update({thing.name:thing})


    while True:
        for thing in items:
        ## lots of other code here, basically just a game loop.
        ## Problem is that the 'turn order' is based on whatever
        ## Python decides the order of arguments is inside "items".
        ## I'd like to be able to sort the dict based on each object's
        ## attributes (ie, highest 'thing.speed' goes first in the while loop)

The problem is when I try to sort “items” based on an attribute of the objects I put into function(), it gives me “AttributeError: ‘str’ object has no attribute ‘attribute'”. Which leads me to believe I’m either unpacking *arg in a lousy way, or I’m trying to do something the wrong way.

while True:
    for thing in sorted(items, key=attrgetter('attribute')):

…doesn’t work either, keeps telling me I’m trying to manipulate a ‘str’ object. What am I not doing here?

  • 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-05-25T13:38:35+00:00Added an answer on May 25, 2026 at 1:38 pm

    arg already is a tuple you can sort by an attribute of each item:

    def function(*args):
        for thing in sorted(args, key=attrgetter('attribute')):
    

    When you iterate over a dict, as sorted is doing, you just get the keys, not the values. So, if you want to use a dict, you need to do:

    def function(*args):
        # or use a dict comprehension on 2.7+ 
        items = dict((thing.name, thing) for thing in args)
    
        # or just items.values on 3+
        for thing in sorted(items.itervalues(), key=attrgetter('attribute')):
    

    to actually sort the args by an attribute. If you want the keys of the dict available as well (not necessary here because the key is also an attribute of the item), use something like:

    for name, thing in sorted(items.iteritems(), key=lambda item: item[1].attribute):
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to sort a mutable array by date. My array contains several dict
I want to create a dict which can be accessed as: d[id_1][id_2][id_3] = amount
I have a dict that I want to convert in JSON using simplejson. How
I've been trying to figure out the best way to write a query to
I want to group by on dict key >>> x [{'a': 10, 'b': 90},
I want to do something like this: class Dictable: def dict(self): raise NotImplementedError class
I want to statically define a mapped array of strings like: var dict =
The xml I want to deal with is like this (from an dict api).
I use a dict as a short-term cache. I want to get a value
want to know why String behaves like value type while using ==. String s1

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.