how to pass elements in a list to a function in a for-loop
<p>I'm trying to print the pressure of each city.  </p>

<p>Instead of using the cities in the list, it is returning the pressure of a city called 'city'. </p>

<p>Super new to this and can't find an answer to something this specific.  thanks!!!!!!</p>

<pre><code>import pyowm
owm = pyowm.OWM('eb68e3b0c908251771e67882d7a8ddff')

city = ["tokyo", "jakarta"]

for i in city:
    city = owm.weather_at_place('city')
    city = city.get_weather()
    print (city.get_pressure()['press'])
</code></pre>

<p>Use better names and things will become clearer:</p>

<pre><code>import pyowm
owm = pyowm.OWM('eb68e3b0c908251771e67882d7a8ddff')

cities = ["tokyo", "jakarta"]

for city in cities:
    weather = owm.weather_at_place(city).get_weather()
    print (weather.get_pressure()['press'])
</code></pre>

<p>Changes and rationales:</p>

<ul>
<li>Changed the list of cities from <code>city</code> to <code>cities</code>. A variable named <code>city</code> should contain a city. Not a list of them.</li>
<li>Changed the iteration variable used by the <code>for</code> loop to <code>city</code> because that is what will be in the variable: the name of one city. <code>i</code> is a poor name because you'd expect it to be an index, not the item, and Python <code>for</code> loops don't work that way. See how nicely <code>for city in cities</code> expresses what is actually happening?</li>
<li>Changed the call to <code>weather_at_place()</code> to use the <em>variable</em> <code>city</code> (a city from the list) rather than the constant string <code>"city"</code>, as the latter would search for a city <em>named City.</em></li>
<li>Chained the calls to <code>weather_at_place()</code> and <code>get_weather()</code> rather than using <code>city</code> to hold the value returned by the former, because the intermediate value is never used. Also eliminates the use of the variable <code>city</code> for something else that is not a city.</li>
<li>Changed the variable that contains the result of <code>get_weather()</code> to <code>weather</code> from <code>city</code> because it contains the weather, not the city.</li>
</ul>

<p>Readability is important, even (especially!) when the person reading the code is the one who wrote it. If you're getting confused <em>now,</em> writing the code for the first time, imagine how confused you'll be when you try to modify the code in six months!</p>

python for-loop