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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:21:55+00:00 2026-06-01T21:21:55+00:00

I’m sorry if this is a question answered elsewhere. Searching through Google and Stackforum

  • 0

I’m sorry if this is a question answered elsewhere. Searching through Google and Stackforum I didn’t find anything from which I could extrapolate the answers; but I feel like part of that is me.

I’m trying to work out lambdas as a concept, and as part of that I’m kinda looking for ways to use it.

SO, if this is a colossally stupid thing to do with lambda from a function standpoint, feel free to let me know and explain. But either way, I still want to know the answer/still want to know how to do this with the python language.

So, for testing purposes I have:

my_test = 'test_name'
testlist = ['test_name', 'test_name_dup', 'test_name_dup_1', 'test_name_dup_3']

I’m looking to use lambda to create one function that loops through and returns the first test_name_# that isn’t in the testlist. The functionality will eventually be applied to filenames, but for testing purposes I had to get away from actually reading the filenames–gave me too many more ways to mess something up.

But my_test has to be able to change, and the test list will be a list of filepaths.

So, I’m looking for a function like:

new_name = lambda x: my_test + '_' + str(x)

But the initial value should be x = 1, and it should continue until new_name is not in testlist. Seems like:

bool(new_name not in testlist)

might be something work with.

But I can’t figure out a way to set the initial x to 1, and have it loop through with (x+1) until the bool is true.

I know this is possible as I’ve found some CRAZY lambda examples out there that are looping through lines in a file. I just couldn’t quite make sense of them (and didn’t have any way to play with them as they were dealing with things outside my programming level.

On a related note, could I add values to the beginning of this loop? (i.e. can I have it check for test_name, then test_name_dup, then test_name_dup_#)?

Thanks in advance for the help! Lambdas (while very cool) totally mess with my head.

  • 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-01T21:21:57+00:00Added an answer on June 1, 2026 at 9:21 pm

    There’s no need to use a lambda in this case, a simple for loop will do:

    my_test  = 'test_name_dup'  
    testlist = ['test_name', 'test_name_dup','test_name_dup_1', 'test_name_dup_3']
    
    for i in xrange(1, len(testlist)):
        if my_test + '_' + str(i) not in testlist:
            break
    
    print my_test + '_' + str(i)
    > test_name_dup_2
    

    If you really, really want to use a lambda for this problem, you’ll also have to learn about itertools, iterators, filters, etc. I’m gonna build on thg435’s answer, writing it in a more idiomatic fashion and explaining it:

    import itertools as it
    
    iterator = it.dropwhile(
        lambda n: '{0}_{1}'.format(my_test, n) in testlist,
        it.count(1))
    
    print my_test + '_' + str(iterator.next())
    > test_name_dup_2
    

    The key to understanding the above solution lies in the dropwhile() procedure. It takes two parameters: a predicate and an iterable, and returns an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.

    For the iterable, I’m passing count(1), an iterator that produces an infinite number of integers starting from 1.

    Then dropwhile() starts to consume the integers until the predicate is false; this is a good opportunity for passing an in-line defined function – and here’s our lambda. It receives each generated integer in turn, checking to see if the string test_name_dup_# is present in the list.

    When the predicate returns false, dropwhile() returns and we can retrieve the value that made it stop by calling next() on it.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,

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.