Splitting String at ',' accounting for No Data

1969
23
01-10-2019 10:44 AM
deleted-user-qpvAI3Fo0MKR
Deactivated User

I've got a table with a field named Maintenance Tasks. I would like to parse the values in this field into separate fields (Task1, Task2 etc.):

I can use:

         None if !MaintenanceTasks! == None else !MaintenanceTasks!.split(',')[0]

and this will parse the first item correctly into Task1, it also accounts for any null values in MaintenanceTasks.

The problem I'm running into is, for example, OID 23 above, when there's just a single item in the MaintenanceTasks field (ReplaceHead).

Any help would be greatly appreciated. FWIW I'll be scripting in Python.

Tags (1)
0 Kudos
23 Replies
DanPatterson_Retired
MVP Emeritus

I hate it when you forget to press Add Comment...

A bit late, but better than never

Josh... a few enumerate things

a = ['a', 'c', 'b', 'd']

for i in range(len(a)):
    print(i)
    
0
1
2
3

for i, j in enumerate(a):
    print("{}, {}".format(i, j))
    
0, a
1, c
2, b
3, d

list(enumerate(a))
[(0, 'a'), (1, 'c'), (2, 'b'), (3, 'd')]



# ---- no need to check the length of something, enumerate is smart


a = []

list(enumerate(a))
[]


# ---- and they return the same thing, but 'enumerate' sounds cooler


for i in range(len(a)):
    print(i)
    

for i in enumerate(a):
    print(i)


# ---- ps... they both returned nothing, but an empty list
    ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JoeBorgione
MVP Emeritus

I need to swap out range(len(someList)) with enumerate(someList) in a couple scripts to get a handle on it. To me, a list of tuples is something of a hybrid between a dictionary and an array...

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If you are used to working with a counter variable to index positions in a list, you can use tuple unpacking in the for loop:

for i,v in enumerate(list):
    pass
deleted-user-qpvAI3Fo0MKR
Deactivated User

Thank you very much, Dan.

If I understand your top example above, the location of the item in the list is being enumerated; location 0 = a, location 1=a and so on. Is this accurate?

In the latter example, both returned nothing as you declared a = [ ].

Very useful, thanks for the education!