1

I have a dictionary in this format:

d = {'type 1':[1,2,3],'type 2':['a','b','c']}

It's a symmetric dictionary, in the sense that for each key I'll always have the same number of elements.

There is a way to loop as if it were rows:

for row in d.rows():
    print row

So I get the output:

[1]: 1, a
[2]: 2, b
[3]: 3, b
Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181

3 Answers3

3

You can zip the .values(), but the order is not guaranteed unless you are using a collections.OrderedDict (or a fairly recent version of PyPy):

for row in zip(*d.values()):
    print(row)

e.g. when I run this, I get

('a', 1)
('b', 2)
('c', 3)

but I could have just as easily gotten:

(1, 'a')
(2, 'b')
(3, 'c')

(and I might get it on future runs if hash randomization is enabled).


If you want a specified order and have a finite set of keys that you know up front, you can just zip those items directly:

zip(d['type 1'], d['type 2'])
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Will this work for d = {'type 1':[1,2,3], 'type 2':['a','b','c'], 'type 4':[4,5,6], 'type 3':['d','e','f'] } What will happen if the dictionary has more elements? – arshovon Jul 08 '16 at 16:54
  • @arsho -- Yes, it'll work for an arbitrary number of elements. – mgilson Jul 08 '16 at 17:19
  • When d = {'type 1':[1,2,3], 'type 2':['a','b','c'], 'type 4':[4,5,6], 'type 3':['d','e','f'] } It generates (4, 'd', 'a', 1) (5, 'e', 'b', 2) (6, 'f', 'c', 3). So it not ordered as the dictionary input. – arshovon Jul 08 '16 at 17:28
  • @arsho -- Right, that's why my answer says "the order is not guaranteed unless ...". In fact, without using a `collections.OrderedDict`, there is no way to guarantee that you order the columns based on the order of the dictionary input. – mgilson Jul 08 '16 at 17:30
1

If you want your rows ordered alphabetically by key you might consider:

zip(*(v for k, v in sorted(d.iteritems())))
# [(1, 'a', 'd', 4), (2, 'b', 'e', 5), (3, 'c', 'f', 6)]

If your dataset is huge then consider itertools.izip or pandas.

import pandas as pd
df = pd.DataFrame(d)
print df.to_string(index=False)

# type 1 type 2 type 3  type 4
#      1      a      d       4
#      2      b      e       5
#      3      c      f       6

print df.to_csv(index=False, header=False)

# 1,a,d,4
# 2,b,e,5
# 3,c,f,6
1

Even you pass the dictionary to OrderedDict you will not get ordered result as dictionary entry. So here tuples of tuple can be a good option.

See the code and output below:

Code (Python 3):

import collections
t = (('type 1',[1,2,3]),
     ('type 2',['a','b','c']),
     ('type 4',[4,5,6]),     
     ('type 3',['d','e','f']))
d = collections.OrderedDict(t)
d_items = list(d.items())
values_list = []
for i in range(len(d_items)):
    values_list.append(d_items[i][1])
values_list_length = len(values_list)
single_list_length = len(values_list[0])
for i in range(0,single_list_length):
    for j in range(0,values_list_length):
        print(values_list[j][i],' ',end='')
    print('')

Output:

1  a  4  d 
2  b  5  e 
3  c  6  f 
arshovon
  • 13,270
  • 9
  • 51
  • 69