0

I have the following list of dictionaries:

[{'Sequence': 'TGACCCTGCTTGGCGATCCCGGCGTTTC', 'Start': '52037', 'Strand': '+', 'End': '52064'}, {'Sequence': 'TGATCGCGCAACTGCAGCGGGAGTTAC', 'Start': '188334', 'Strand': '+', 'End': '188360'}, {'Sequence': 'TGATCCGGCACTCGTTGGAGTTCGTATC', 'Start': '245882', 'Strand': '+', 'End': '245909'}, {'Sequence': 'TGACCTTGGTCAGATCGATGACCGTAAT', 'Start': '318422', 'Strand': '+', 'End': '318449'}, {'Sequence': 'TGACCCGCGTATTTTGGAGCAGAAGTATC', 'Start': '343421', 'Strand': '+', 'End': '343449'}, {'Sequence': 'TGATCCGGCACTCGTTGGAGTTCGTATC', 'Start': '359576', 'Strand': '+', 'End': '359603'}, {'Sequence': 'TGATCGGCAATTCCTATGGCAAGTATC', 'Start': '457457', 'Strand': '+', 'End': '457483'}, {'Sequence': 'TGATCGAGGCGCCAGTTGTGCCCGTATT', 'Start': '627296', 'Strand': '+', 'End': '627323'}, {'Sequence': 'TGATCCAAGTGAACCCCCGCCCAGTAAA', 'Start': '637265', 'Strand': '+', 'End': '637292'}, {'Sequence': 'TGACCGGAAGACCGCCGTCGAGCGTATC', 'Start': '829035', 'Strand': '+', 'End': '829062'}, {'Sequence': 'TGATCGCGGCACCGACACCGGTCGTAAT', 'Start': '864440', 'Strand': '+', 'End': '864467'}, {'Sequence': 'TGATCGTTTCCGTGCTCGGAGCGTATC', 'Start': '934160', 'Strand': '+', 'End': '934186'}, {'Sequence': 'TGACCTGGCTCGAGGCCTGAGGAGTAAA', 'Start': '1162006', 'Strand': '+', 'End': '1162033'}, {'Sequence': 'TGACCCGCCGCAACATCCCCTTCGTAAA', 'Start': '1294515', 'Strand': '+', 'End': '1294542'}]

I would like to acquire the following output (redirecting prints via sys.stdout):

Start:1234  End:5678  Strand:+  Sequence:ABCDEFG
...

So far, my idea involves looping through the list and using the get-method to get the value of the key and then do a manual printout, basically like this:

for item in dictList:
    print("Start"+item.get('Start'))
    print("End"+item.get('End'))
    ...

I am not sure how to deal with the newlines though. I read about sys.stdout flush to avoid newlines, but since I do not want to avoid all newlines, I am not sure if this would suit my aim.

Is there a smarter/shorter way to do this?

Update

Thanks to Aleksey, I thought of changing my print statement to:

for item in dictList:
    print("Start"+str(item.get('Start'))+"  "+"End"+str(item.get('End'))+"  "+"Strand"+str(item.get('Strand'))+"  "+"Sequence: "+str(item.get('Sequence')))

Update2

so far, I am sticking to this:

print(ecf+"  "+replicon+"  -  "+"Validated results: Total number of hits: "+str(len(finalList)))
print("")

for item in finalList:
    print("Start: "+str(item.get('Start'))+"  "+"End: "+str(item.get('End'))+"  "+"Strand: "+str(item.get('Strand'))+"  "+"Sequence: "+str(item.get('Sequence')))

which prints:

ECF02  pSymA  -  Validated results: Total number of hits: 284

Start: 2876  End: 2903  Strand: +  Sequence: GAACTACAGGAAGTTCATGGTCTCCTTC
Start: 2876  End: 2900  Strand: +  Sequence: GAACTACAGGAAGTTCATGGTCTCC
Start: 9214  End: 9238  Strand: +  Sequence: GAACGTCGGGCGCAAGAGCCGCTTT
...

please note: dictList was replaced by finalList. Due to parts of my script running before those prints

Shushiro
  • 577
  • 1
  • 9
  • 32

3 Answers3

1

Avoid new lines by simply specifying the ending with:

print("Start"+item.get('Start'), end=" ")

end=" " will continue your string without new lines

Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
  • ah, so I just thought, i could also do this: print("Start"+item.get('Start)'+" "+"End"+item.get('End') ... etc) and write it all in one big print statement. Then the fact, that a new print makes a newline would be useful – Shushiro Dec 07 '17 at 09:54
1

You could use .format_map(each_dic) or later Python 3.5+ added alternative .format(**each_dic). Uses each dict in the list in each print with formatting.

for each_dic in dictList:
    print('Sequence: {Sequence:<29} '
          'Start: {Start:>7} '
          'Strand: {Strand} '
          'End: {End:>7}'.format_map(each_dic))

Look at Python help page: string - Common string operations for further reference and examples.

michael_heath
  • 5,262
  • 2
  • 12
  • 22
0

Taken from my update, inspired by Aleksey Solovey

print(ecf+"  "+replicon+"  -  "+"Validated results: Total number of hits: "+str(len(finalList)))
print("")

for item in finalList:
    print("Start: "+str(item.get('Start'))+"  "+"End: "+str(item.get('End'))+"  "+"Strand: "+str(item.get('Strand'))+"  "+"Sequence: "+str(item.get('Sequence')))
Shushiro
  • 577
  • 1
  • 9
  • 32