<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: list comprehension to evaluate each element of a list in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700643#M75225</link>
    <description>&lt;P&gt;The problem is that after building&amp;nbsp; datelist, you still have to write the values back to line. Right now you're updating the list but never putting the results back into the row, so the original line[5] and line[14] are unchanged and still hold ''&lt;/P&gt;&lt;LI-CODE lang="c"&gt;datelist = [line[5], line[14]]
datelist = [None if date == '' else date for date in datelist]

# You're missing this:
line[5], line[14] = datelist&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;line[5], line[14] = (None if line[i] == '' else line[i] for i in (5, 14))&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 07 May 2026 15:34:31 GMT</pubDate>
    <dc:creator>TonyAlmeida</dc:creator>
    <dc:date>2026-05-07T15:34:31Z</dc:date>
    <item>
      <title>list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700624#M75222</link>
      <description>&lt;P&gt;In my project I am reading a .csv which has two date values, these are at positions 5 and 14 in the row. The date values can either be null (indicated as '' in the .csv) or they can hold an actual date in the format of mm/dd/yyyy. In order to insert these rows into my geodatabase, I have to convert the '' value to a null, otherwise the value is not recognized as valid for a date field.&lt;/P&gt;&lt;P&gt;I can get the following code to work for each field:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;empty = line[5]
if empty == '';
  line[5] = None
else:
  line[5]=empty&lt;/LI-CODE&gt;&lt;P&gt;However, I was thinking I could implement a list comprehension to accomplish this more efficiently by using the following:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;datelist = [line[5],line[14]]
datelist = [None if date == '' else date for date in datelist]&lt;/LI-CODE&gt;&lt;P&gt;This doesn't seem to work, I get an error indicating a type mismatch with a date field.&lt;/P&gt;&lt;P&gt;Any thoughts on what I am misunderstanding or how I can approach this. Since I have a working solution its not critical so I am just trying to improve my skills and knowledge a bit here. Though since I am iterating over 20K records, I suppose every bit of efficiency helps.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 14:45:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700624#M75222</guid>
      <dc:creator>clt_cabq</dc:creator>
      <dc:date>2026-05-07T14:45:11Z</dc:date>
    </item>
    <item>
      <title>Re: list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700632#M75223</link>
      <description>&lt;P&gt;Is your null value two single quotes or an empty string?&lt;/P&gt;&lt;LI-CODE lang="c"&gt;date1,date2
'',dd/mm/yyy

-- OR --
date1,date2
,dd/mm/yyy&lt;/LI-CODE&gt;&lt;P&gt;The former will evaluate to "''" when read, so your check is incorrect:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; # version 1
&amp;gt;&amp;gt;&amp;gt; print(row[4])
''
&amp;gt;&amp;gt;&amp;gt; repr(row[4])
'"\'\'"'
&amp;gt;&amp;gt;&amp;gt; # version 2
&amp;gt;&amp;gt;&amp;gt; print(row[4])

&amp;gt;&amp;gt;&amp;gt; repr(row[4])
"''"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To fix it that way, just modify your condition to check for the literal '' string:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;datelist = [line[5],line[14]]
datelist = [None if date == "''" else date for date in datelist]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can also do some fun stuff with the csv.DictReader if you want to avoid indexing:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from collections.abc import Iterator
from typing import Any
from pathlib import Path
from csv import DictReader

def format(line: str, *, sep: str=',') -&amp;gt; list[str]:
    return line.strip().split(sep)

def get_dates(fl: Path) -&amp;gt; Iterator[tuple[str | None, str | None]]:
    with fl.open('rt') as data:
        headers = format(data.readline())
        for line in data:
            row = dict(zip(headers, format(line)))
            yield (
                None if (dt:=row.get('date1')) == "''" else dt, 
                None if (dt:=row.get('date2')) == "''" else dt,
            )

def format_csv(fl: Path) -&amp;gt; Iterator[dict[str, Any]]:
    with fl.open('rt') as data:
        headers = format(data.readline())
        for row in DictReader(data, headers):
            row['date1'] = None if (dt:=row.get('date1')) == "''" else dt
            row['date2'] = None if (dt:=row.get('date1')) == "''" else dt
            yield row&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This creates a generator for the CSV rows, which means you are only loading in one row at a time, which lets you filter them before adding them to a memory list. If you have a ton of data this can prevent you from loading all of it into memory every time you want to check for something:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Initialize the generator (nothing is loaded in)            
reader = format_csv(Path('data.csv'))

# Create a list that contains only the rows of the
# csv where both date fields are populated
valid_rows = [
    row for row in reader 
    if row['date1'] and row['date2']
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Additional Note on Generators:&lt;/P&gt;&lt;P&gt;Once a generator is exhausted (it's been iterated with a comprehension/for loop), It can't be iterated again:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; x = (i for i in range(10)) # generator comprehension syntax
&amp;gt;&amp;gt;&amp;gt; list(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
&amp;gt;&amp;gt;&amp;gt; list(x)
[]
&amp;gt;&amp;gt;&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 17:23:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700632#M75223</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2026-05-07T17:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700634#M75224</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/607017"&gt;@HaydenWelch&lt;/a&gt;&amp;nbsp;thanks! this is great stuff, I'll see if the quoting example you provide works and report back.&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 15:17:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700634#M75224</guid>
      <dc:creator>clt_cabq</dc:creator>
      <dc:date>2026-05-07T15:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700643#M75225</link>
      <description>&lt;P&gt;The problem is that after building&amp;nbsp; datelist, you still have to write the values back to line. Right now you're updating the list but never putting the results back into the row, so the original line[5] and line[14] are unchanged and still hold ''&lt;/P&gt;&lt;LI-CODE lang="c"&gt;datelist = [line[5], line[14]]
datelist = [None if date == '' else date for date in datelist]

# You're missing this:
line[5], line[14] = datelist&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;line[5], line[14] = (None if line[i] == '' else line[i] for i in (5, 14))&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 07 May 2026 15:34:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700643#M75225</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2026-05-07T15:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700649#M75226</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3265"&gt;@TonyAlmeida&lt;/a&gt;&amp;nbsp;I'll have to test this out - this pattern seems to work in another case where I am converting null coordinates to 0s, so it is unclear to me why it wouldn't work in this case also. For context perhaps it helps to understand that in reading the .csv I'm doing this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;readcsv = csv.reader(txtfile,delimiter=',')
next(readcsv)
  for line in readcsv:
  new_line = []
... code to update values...
new_line.extend(line)
insertcursor.insertRow(new_line)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 15:46:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700649#M75226</guid>
      <dc:creator>clt_cabq</dc:creator>
      <dc:date>2026-05-07T15:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700654#M75227</link>
      <description>&lt;P&gt;That's the issue. You're building new_line from line after your modifications, but if the list comprehension results aren't being written back to line before the extend, the original '' values are what get copied into new_line.&lt;/P&gt;&lt;P&gt;Since you're constructing new_line anyway, you don't need to write back to line at all. Just build the conversion into however you're populating new_line. The simplest approach:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;for line in readcsv:
    line[5]  = None if line[5]  == '' else line[5]
    line[14] = None if line[14] == '' else line[14]
    new_line = []
    new_line.extend(line)
    insertcursor.insertRow(new_line)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or if you want to keep it as a comprehension and avoid the writeback issue entirely, apply it directly when extending:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;for line in readcsv:
    new_line = [None if (i in (5, 14) and line[i] == '') else line[i] for i in range(len(line))]
    insertcursor.insertRow(new_line)&lt;/LI-CODE&gt;&lt;P&gt;That rebuilds the whole row in one pass and drops the extend entirely. Cleaner, and no ambiguity about whether the mutation happened before or after the copy.&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 16:03:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700654#M75227</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2026-05-07T16:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700657#M75228</link>
      <description>&lt;P&gt;If there's mutation of the line, I think my generator solution is probably the best bet. It turns the operation into multiple steps so you process the line, then convert those processed rows to a list:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def process(csv: Path) -&amp;gt; Iterator[list[Any]]:
    ... do line processing
    yield processed_line

valid_lines = [l for l in process(Path('data.csv')) if &amp;lt;predicate&amp;gt;]
invalid_lines = [l for l in process(Path('data.csv')) if not &amp;lt;predicate&amp;gt;]

-- OR --

valid_lines = []
invalid_lines = []
for line in process(Path('data.csv')):
    if not &amp;lt;predicate&amp;gt;:
        invalid_lines.append(line)
        continue
    valid_lines.append(line)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Trying to mutate a list of arbitrary values in a comprehension is a pain. Ideally you want a mapping of values to fields, or just a direct list[i] = &amp;lt;some conversion of list[i]&amp;gt;.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 16:15:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700657#M75228</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2026-05-07T16:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700658#M75229</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/607017"&gt;@HaydenWelch&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/3265"&gt;@TonyAlmeida&lt;/a&gt;&amp;nbsp;appreciate your help and suggestions... I'll play with these different approaches and see which seems to work best for me!&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 16:27:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700658#M75229</guid>
      <dc:creator>clt_cabq</dc:creator>
      <dc:date>2026-05-07T16:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700756#M75230</link>
      <description>&lt;P&gt;Just putting in my two cents into this conversation but it could also be that the mapping of some fields could result in some errors when populating from a csv to a dbf or feature table in a database.&lt;/P&gt;&lt;P&gt;I would utilize&amp;nbsp;functions that&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/607017"&gt;@HaydenWelch&lt;/a&gt;&amp;nbsp;provided to extract the rows in the csv while also doing the same for the table you want to insert records into. You can then compare both sets of values to see if there is any mismatch in datatypes so that you can map the required values.&lt;/P&gt;&lt;P&gt;It may also require the use of datetime to convert any text that indicates a time to an actual datetime value if that is another issue you are running into. datetime can also be validated using &lt;STRONG&gt;isinstance(value, datetime)&lt;/STRONG&gt; to verify that the value is a datetime value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 20:36:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700756#M75230</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2026-05-07T20:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: list comprehension to evaluate each element of a list</title>
      <link>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700769#M75232</link>
      <description>&lt;P&gt;If the date is stored as a string, you will need to convert it to a datetime object before inserting it into a date field I believe. At least if it's not stored in an ISO format&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 21:19:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-comprehension-to-evaluate-each-element-of-a/m-p/1700769#M75232</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2026-05-07T21:19:45Z</dc:date>
    </item>
  </channel>
</rss>

