title: Multiple with statements in Python 2.7 using a list comprehension
body: <h1>Question:</h1>
  <p>I am interested in doing a list comprehension <strong>inside</strong> a Python <code>with</code> statement, so that I can open multiple context managers at the same time with minimal syntax. I am looking for answers that work with <strong>Python 2.7</strong>.</p>
  <p>Consider the following code example. I want to use the <code>with</code> statement on variables in an arbitrarily-long list <strong>at the same time</strong>, preferably in a syntactically-clean fashion.</p>
  <pre><code>def do_something(*args):
      contexts = {}
      with [open(arg) as contexts[str(i)] for i, arg in enumerate(args)]:
          do_another_thing(contexts)
  
  do_something(&quot;file1.txt&quot;, &quot;file2.txt&quot;)
  </code></pre>
  <p>Does anybody know if there is a way to involve a list comprehension inside of a <code>with</code> statement in <strong>Python 2.7</strong>?</p>
  <hr />
  <h1>Answers to similar questions:</h1>
  <p>Here are some things I've already looked at, with an explanation of why they do not suit my purposes:</p>
  <p>For <strong>Python 2.6-</strong>, I could use <code>contextlib.nested</code> to accomplish this a bit like:</p>
  <pre><code>def do_something(*args):
      contexts = {}
      with nested(*[open(arg) for arg in args]) as [contexts[str(i)] for i in range(len(args))]:
          do_another_thing(contexts)
  </code></pre>
  <p>However, this is deprecated in <strong>Python 2.7+</strong>, so I am assuming it is bad practice to use.</p>
  <p>Instead, the new syntax was given on <a href="https://stackoverflow.com/questions/893333/multiple-variables-in-python-with-statement">this SO answer</a>, as well as <a href="https://stackoverflow.com/questions/3024925/python-create-a-with-block-on-several-context-managers/3024953#3024953">this SO answer</a>:</p>
  <pre><code>with A() as a, B() as b, C() as c:
      doSomething(a,b,c)
  </code></pre>
  <p>However, I need to be able to deal with an arbitrary input list, as in the example I gave above. This is why I favour list comprehension.</p>
  <p>For <strong>Python 3.3+</strong>, <a href="https://stackoverflow.com/a/3025119/2689923">this SO answer</a> described how this could be accomplished by using <code>ExitStack</code>. However, I am working in Python 2.7.</p>
  <p>There is also <a href="http://metapython.blogspot.com/2010/12/multiple-contests-in-with-statement-not.html" rel="nofollow noreferrer">this solution</a>, but I would prefer to not write my own class to accomplish this.</p>
  <p>Is there any hope of combining a list comprehension and a <code>with</code> statement in Python 2.7?</p>
  <p><strong>Update 1-3: Updated example to better emphasize the functionality I am looking for</strong></p>
  <p><strong>Update 4: Found another <a href="https://stackoverflow.com/questions/16083791/alternative-to-contextlib-nested-with-variable-number-of-context-managers">similar question</a></strong>. This one has an answer which also suggests <code>ExitStack</code>, a function that is not available in 2.7.</p>