0

New to python and for this example list

lst = ['<name>bob</name>', '<job>doctor</job>', '<gender>male</gender>', '<name>susan</name>', '<job>teacher</job>', '<gender>female</gender>', '<name>john</name>', '<gender>male</gender>']

There are 3 categories of name, job, and gender. I would want those 3 categories to be on the same line which would look like <name>bob</name>, <job>doctor</job>, <gender>male</gender>

My actual list is really big with 10 categories I would want to be on the same line. I am also trying to figure out a way where if one of the categories is not in the list, it would print something like N/A to indicate that it is not in the list

for example I would want it to look like

<name>bob</name>, <job>doctor</job>, <gender>male</gender>
<name>susan</name>, <job>teacher</job>, <gender>female</gender>
<name>john</name>, N/A, <gender>male</gender> 

What would be the best way to do this?

  • are they in the correct order in the list? – drum Jun 22 '20 at 16:07
  • yes I open and read a file and put all of the categories I want in one big list so it should be in order. Just looking at the list after printing it looks like everything is in order. – codeclimb00 Jun 22 '20 at 16:08
  • Does this answer your question? [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – drum Jun 22 '20 at 16:16
  • The only thing I am kind of unsure of is I don't know how long my list will be and I don't know if all the categories will be there. I'm pretty new to python, but I think I can find something useful here. Thank you! – codeclimb00 Jun 22 '20 at 16:35

1 Answers1

0

This is one way to do it. This would handle any length list, and guarantee grouping no matter how long the lists are as long as they are in the correct order.

Updated to convert to dict, so you can test for key existence.

lst = ['<name>bob</name>', '<job>doctor</job>', '<gender>male</gender>', '<name>susan</name>', '<job>teacher</job>', '<gender>female</gender>', '<name>john</name>', '<gender>male</gender>']

newlst = []
tmplist = {}
for item in lst:
    value = item.split('>')[1].split('<')[0]
    key = item.split('<')[1].split('>')[0]
    if '<name>' in item:
        if tmplist:
            newlst.append(tmplist)
            tmplist = {}
    tmplist[key] = value

#handle the remaining items left over in the list
if tmplist:
    newlst.append(tmplist)

print(newlst)

#test for existance
for each in newlst:
    print(each.get('job', 'N/A'))
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
  • The problem with this is that it wouldn't print something like N/A if a category like job is not next to a name. It looks like it would actually take the job of the next '' and put that job with the previous name if that makes sense. – codeclimb00 Jun 22 '20 at 16:34
  • Well then you would loop through the list at the end and look for missing keys. Honestly I would convert all of this into a dict. – eatmeimadanish Jun 22 '20 at 17:58