1

I am trying to extract certain values from a dictionary and display it a specific way. I'll show you my code example below:

dict = [{'Titel': 'Rush', 'Name': 'Floris', 'Starttime': '20:30', 'Email': 'Floris@email.com', 'Supplier': 'RTL8', 'Surname': 'Cake', 'Code': 'ABC123'},
 {'Titel': 'Rush', 'Voornaam': 'Jaron', 'Starttime': '20:30', 'Email': 'JaronPie@email.com', 'Supplier': 'RTL8', 'Surname': 'Pie', 'Code': 'XYZ123'},
 {'Titel': 'Underneath', 'Name': 'Klaas', 'Starttime': '04:00', 'Email': 'Klassieboy@gmail.com', 'Supplier': 'RTL8', 'Surname': 'Klassie', 'Code': 'fbhwuq8674'}]

That is my dictionary. What I want as output is:

Titel, Starttime,
Surname, Name, Email

So it would look like this:

Rush, 20:30, 
Cake, Floris, Floris@email.com
Pie, Jaron, JaronPie@email.com

Underneath, 04:00,
Klassie, Klaas, Klassieboy@gmail.com
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Cake
  • 493
  • 7
  • 16

3 Answers3

3

I used VKS's code to build it thanks VKS :P

Using groupy,list comprehension and sting formatting

Code:

from itertools import groupby
k = [{'Titel': 'Rush', 'Name': 'Floris', 'Starttime': '20:30', 'Email':      'Floris@email.com', 'Supplier': 'RTL8', 'Surname': 'Cake', 'Code': 'ABC123'},
{'Titel': 'Rush', 'Voornaam': 'Jaron', 'Starttime': '20:30', 'Email': 'JaronPie@email.com', 'Supplier': 'RTL8', 'Surname': 'Pie', 'Code': 'XYZ123'},
{'Titel': 'Underneath', 'Name': 'Klaas', 'Starttime': '04:00', 'Email': 'Klassieboy@gmail.com', 'Supplier': 'RTL8', 'Surname': 'Klassie', 'Code': 'fbhwuq8674'}]

lst=[(i["Titel"],i["Starttime"],i["Surname"],i.get("Name","None"), i["Email"]) for i in k]
lst.sort(key=lambda x:(x[0],x[1]))
for key,groups in groupby(lst,key=lambda x:(x[0],x[1])):
    print "{}\t{}".format(key[0],key[1])
    for value in groups:
        print "{}\t{}\t{}".format(value[2],value[3],value[4])
    print ""

Output:

Rush    20:30
Cake    Floris  Floris@email.com
Pie     None    JaronPie@email.com

Underneath      04:00
Klassie Klaas   Klassieboy@gmail.com
Community
  • 1
  • 1
The6thSense
  • 8,103
  • 8
  • 31
  • 65
1
k = [{'Titel': 'Rush', 'Name': 'Floris', 'Starttime': '20:30', 'Email':      'Floris@email.com', 'Supplier': 'RTL8', 'Surname': 'Cake', 'Code': 'ABC123'},
 {'Titel': 'Rush', 'Voornaam': 'Jaron', 'Starttime': '20:30', 'Email': 'JaronPie@email.com', 'Supplier': 'RTL8', 'Surname': 'Pie', 'Code': 'XYZ123'},
 {'Titel': 'Underneath', 'Name': 'Klaas', 'Starttime': '04:00', 'Email': 'Klassieboy@gmail.com', 'Supplier': 'RTL8', 'Surname': 'Klassie', 'Code': 'fbhwuq8674'}]

print [(i["Titel"],i["Starttime"],i["Surname"], i["Email"]) for i in k]

You can use this and you will get tuple of all info you want.

vks
  • 67,027
  • 10
  • 91
  • 124
1
def print_my_d(d):

     print(d[0]['Titel'], d[0]['Starttime'])
     for l in d[:2]:
         print(l['Surname'], l['Name'], l['Email'])
     print()
     print(d[2]['Titel'],d[2]['Starttime'])
     print("{}, {}, {}".format(d[2]['Surname'],d[2]['Name'], d[2]['Email']))


print_my_d(d)

Rush 20:30
Cake Floris Floris@email.com
Pie Jaron JaronPie@email.com

Underneath 04:00
Klassie, Klaas, Klassieboy@gmail.com
LetzerWille
  • 5,355
  • 4
  • 23
  • 26
  • Than you for replying @LetzerWille. You're very close, but it's not quite what I want. I want the Titel + Time to ONLY be printed once and all of the values that match that titel, within the dict, to be printed underneath. – Cake Oct 15 '15 at 10:01
  • @Cake. Changed to reflect what you need – LetzerWille Oct 15 '15 at 10:15
  • @LetzerWille still some things are missing – The6thSense Oct 15 '15 at 10:19
  • @LetzerWille happy to help but this will not work if the sample cases changes that is instead of three element list the user can get one element list it is best to use group by – The6thSense Oct 15 '15 at 10:39
  • @VigneshKalai that's true. but even key naming can change. Nothing is eternal. I assume it is a toy for OP and not a production stuff. Meanwhile I will look closer at you general solution ... I like it ... – LetzerWille Oct 15 '15 at 10:43
  • @LetzerWille agreed :) and happy that you liked it :) – The6thSense Oct 15 '15 at 10:44