pragy
pragy

Reputation: 1

python converting multiple dict with same keys to one line CSV

Trying in python to create single line output from multiple dict with same keys.

[{u'Value': u'nine', u'Key': u'nine'}, 
 {u'Value': u'six', u'Key': u'six'}, 
 {u'Value': u'four', u'Key': u'four'}, 
 {u'Value': u'one', u'Key': u'one'}, 
 {u'Value': u'seven', u'Key': u'seven'}, 
 {u'Value': u'ten', u'Key': u'ten'}, 
 {u'Value': u'two', u'Key': u'two'},
 {u'Value': u'three', u'Key': u'three'}, 
 {u'Value': u'five', u'Key': u'five'}, 
 {u'Value': u'eight', u'Key': u'eight'}]

Output I'm looking for:

  1. nine nine six six four four one one...... eight eight or

  2. nine:nine six:six.............eight:eight

I tried various options single expression

Upvotes: 0

Views: 108

Answers (4)

kjmerf
kjmerf

Reputation: 4345

Here is an alternative:

data = [
    {u'Value': u'nine', u'Key': u'nine'}, {u'Value': u'six', u'Key': u'six'}, 
    {u'Value': u'four', u'Key': u'four'}, {u'Value': u'one', u'Key': u'one'}, 
    {u'Value': u'seven', u'Key': u'seven'}, {u'Value': u'ten', u'Key': u'ten'}, 
    {u'Value': u'two', u'Key': u'two'}, {u'Value': u'three', u'Key': u'three'}, 
    {u'Value': u'five', u'Key': u'five'}, {u'Value': u'eight', u'Key': u'eight'}
]

s = ''

for x in data:
    s += x['Value'] + ':' + x['Key'] + ' '

s = s[:-1] #this remove the last space from the string

print(s)

#output

nine:nine six:six four:four one:one seven:seven ten:ten two:two three:three five:five eight:eight

Upvotes: 0

Peter Pesch
Peter Pesch

Reputation: 643

If you are working in python 2, the answer is simple:

End your print statements with a comma. And at the end of the program, add a single empty print statement. This will make sure that the single line gets printed.

Upvotes: 0

PM 2Ring
PM 2Ring

Reputation: 55489

You can easily do that with a list comprehension and two .join calls.

data = [
    {u'Value': u'nine', u'Key': u'nine'}, {u'Value': u'six', u'Key': u'six'}, 
    {u'Value': u'four', u'Key': u'four'}, {u'Value': u'one', u'Key': u'one'}, 
    {u'Value': u'seven', u'Key': u'seven'}, {u'Value': u'ten', u'Key': u'ten'}, 
    {u'Value': u'two', u'Key': u'two'}, {u'Value': u'three', u'Key': u'three'}, 
    {u'Value': u'five', u'Key': u'five'}, {u'Value': u'eight', u'Key': u'eight'}
]

row = ' '.join([':'.join([d[u'Key'], d[u'Value']]) for d in data])
print(row)

output

nine:nine six:six four:four one:one seven:seven ten:ten two:two three:three five:five eight:eight

A variation of that technique can be used if you don't actually want the colons:

row = ' '.join([s for d in data for s in ([d[u'Key'], d[u'Value']])])
print(row)

output

nine nine six six four four one one seven seven ten ten two two three three five five eight eight

This is probably slightly faster, since it requires only a single .join call.

Upvotes: 2

Ajax1234
Ajax1234

Reputation: 71461

If you are trying to condense the list of dictionaries to a single dictionary:

from collections import defaultdict
d = defaultdict(list)
s = [{u'Value': u'nine', u'Key': u'nine'}, {u'Value': u'six', u'Key': u'six'}, {u'Value': u'four', u'Key': u'four'}, {u'Value': u'one', u'Key': u'one'}, {u'Value': u'seven', u'Key': u'seven'}, {u'Value': u'ten', u'Key': u'ten'}, {u'Value': u'two', u'Key': u'two'}, {u'Value': u'three', u'Key': u'three'}, {u'Value': u'five', u'Key': u'five'}, {u'Value': u'eight', u'Key': u'eight'}]
for i in s:
   d['Value'].append(i['Value'])
   d['Key'].append(i['Key'])

print(dict(d))

Output:

{'Key': [u'nine', u'six', u'four', u'one', u'seven', u'ten', u'two', u'three', u'five', u'eight'], 'Value': [u'nine', u'six', u'four', u'one', u'seven', u'ten', u'two', u'three', u'five', u'eight']}

Upvotes: 0

Related Questions