title: Python Lambda Count/Loop Function
body: <p>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.</p>
  
  <p>I'm trying to work out lambdas as a concept, and as part of that I'm kinda looking for ways to use it.</p>
  
  <p>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.</p>
  
  <p>So, for testing purposes I have:</p>
  
  <pre><code>my_test = 'test_name'
  testlist = ['test_name', 'test_name_dup', 'test_name_dup_1', 'test_name_dup_3']
  </code></pre>
  
  <p>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.</p>
  
  <p>But my_test has to be able to change, and the test list will be a list of filepaths.</p>
  
  <p>So, I'm looking for a function like:</p>
  
  <pre><code>new_name = lambda x: my_test + '_' + str(x)
  </code></pre>
  
  <p>But the initial value should be x = 1, and it should continue until new_name is not in testlist.  Seems like:</p>
  
  <pre><code>bool(new_name not in testlist)
  </code></pre>
  
  <p>might be something work with.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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_#)?</p>
  
  <p>Thanks in advance for the help!  Lambdas (while very cool) totally mess with my head.</p>