1

I'd like to know how to parse (or split) and element of a list?

I have a list of lists (of string) such as:

resultList =  [['TWP-883 PASS'], ['TWP-1080 PASS'], ['TWP-1081 PASS']]

where:

resultList[0] = ['TWP-883 PASS']
resultList[1] = ['TWP-1080 PASS']

essentially, I need a variable for the two entries in each element of the list. For example:

issueId = 'TWP-883'
status = 'PASS'

What would allow for iterating through this list and parsing such as above?

5 Answers 5

5

Well that's as simple as:

# You can also assign as you iterate as suggested in the comments.
for issue, status in resultList:
    print issue, status

This outputs

TWP-883 PASS
TWP-1080  PASS
TWP-1081  PASS
TWP-1082  PASS
TWP-884  FAIL
TWP-885  PASS

Here's another example:

>>> x = [1, 2] # or (1, 2), or '12' works with collections
>>> y, z = x
>>> y
1
>>> z
2
>>> 

Incidentally, in Python 3.x, you can also do:

In [1]: x = [1, 2, 3, 4]
In [2]: y, z, *rest = x
In [3]: y
Out[3]: 1
In [4]: z
Out[4]: 2
In [5]: rest
Out[5]: [3, 4]
Sign up to request clarification or add additional context in comments.

3 Comments

or just for issueId,status in resultList (+1 all the same)
thanks! This works great to iterate through the list with variable assignments
@webcrew, no worries, please accept the answer if it answers your question ;)
1

Note: I changed this answer to reflect an edit of the question. Specifically, I added a split() to separate the strings in the nested lists into two strings (issueId and status).


I would use list and dictionary comprehensions to turn your list of lists into a list of dictionaries with the keys issueId and status:

resultList =  [['TWP-883 PASS'], ['TWP-1080 PASS'], ['TWP-1081 PASS']]

result_dicts = [{("issueId","status")[x[0]]:x[1] for x in enumerate(lst[0].split())} for lst in resultList]

Lookups can now be done in this way:

>>> result_dicts[0]["status"]
'PASS'
>>> result_dicts[0]["issueId"]
'TWP-883'
>>> result_dicts[1]
{'status': 'PASS', 'issueId': 'TWP-1080'}
>>> 

To declare variables for each value in each dictionary in the list and print them, use the code below:

for entry in result_dicts:
    issueId = entry["issueId"]
    status = entry["status"]
    print("The status of {0: <10} is {1}".format(issueId, status))

Output:

The status of TWP-883    is PASS
The status of TWP-1080   is PASS
The status of TWP-1081   is PASS

2 Comments

how would you unpack each item?
I've updated the question. Your assumptions in 'UPDATE' are correct. Thanks, I appreciate your assistance
1

You just need a simple for loop that exploits Python's tuple unpacking machinery.

for issueId, status in resultList:
    # Do stuff with issueId and status

Comments

0

If you wish to do more transformations later, use a genexp,

g = (func(issueid, status) for issueid, status in resultList) # func returns non-None objects

If you just want to consume the iterable,

for issueid, status in resultList:
    # print(issueid, status)
    # whatever

Comments

0

You can get the list of the strings by

issueId = [y for x in resultList for (i,y) in enumerate(x.split()) if(i%2==0)]
status = [y for x in resultList for (i,y) in enumerate(x.split()) if(i%2==1)]

to go through every issueID and corrosponding status you can use

for id,st in zip(issueId,status):
    print(id, " : ", st)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.