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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:07:24+00:00 2026-06-11T22:07:24+00:00

Pretty new to Python and have been playing around with lists and the sort

  • 0

Pretty new to Python and have been playing around with lists and the sort member. My question is if I have a list of random strings (all strings of equal length):

li=["hgjtud", "iernfd", "scoeps", "stiiss", "stripe"]

and now I want to sort that list according to some rank which I define with the following function:

def rank_string(str1,str2):
    rank=0
    for i in range(len(str1)): #all strings will be checked to be equal length
        if (str1[i]==str2[i]):
            rank += 1
    return rank

and now I want to sort my list using this function together with a target string, so I try the following:

target_string="stripe"
df = lambda x:rank_string(x,target_string)
sorted_list = resultlist.sort(key=df)

I was of the impression that all list values will be given a key after one pass of the ranking function and thereafter be sorted according to this? This runs, but sorted_list has value None. I assume then that being a n00b, I am missing something fundamental. What? 🙂

Thanks for the help in advance.

  • 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-11T22:07:25+00:00Added an answer on June 11, 2026 at 10:07 pm

    The .sort() method sorts in-place and doesn’t return anything.

    Use sorted() instead if you want the sorted list returned and the original input list left untouched.

    >>> a = [2, 3, 1]
    >>> a.sort()
    >>> a
    [1, 2, 3]
    >>> b = [2, 3, 1]
    >>> sorted(b)
    [1, 2, 3]
    >>> b
    [2, 3, 1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to Python programming so I have this question: How can I
I'm pretty new to Python and I have a situation where I have a
I'm pretty new to Python and am completely confused by .join() which I have
I am new to python and I have been trying to change my php
I'm pretty new to Python and, in writing an app, have ended up with
I am pretty new in python, and I have a problem I don't know
I'm pretty new to the world of Django / Python. I have a the
I'm still pretty new to Python. I have a Django website with several apps
I'm pretty new to python, using python 2.7. I have to read in a
Hi I'm pretty new to Python, so I'm not really aware of all the

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.