[Tutor] creating dictionary from a list

Saad Bin Javed sbjaved at gmail.com
Sat Apr 13 01:52:52 CEST 2013


Hi, I'm using a script to fetch my calendar events. I split the output 
at newline which produced a list 'lst'. I'm trying to clean it up and 
create a dictionary with date:event key value pairs. However this is 
throwing up a bunch of errors.

lst = ['', 'Thu Apr 04           Weigh In', '', 'Sat Apr 06 
Collect NIC', \
'                     Finish PTI Video', '', 'Wed Apr 10           Serum 
uric acid test', \
'', 'Sat Apr 13   1:00pm  Get flag from dhariwal', '', 'Sun Apr 14 
      Louis CK Oh My God', '', '']

dict = {}
same_day = ''
for x in lst:
	c = x.split('           ')
	if c[0] is '':
		for q in c:
			if q is not '':
				dict.update({same_day: dict[same_day] + ', ' + q.strip()})
				break
	else:
		if c[0].find('  '):
			print c[0]
			a = c[0].split('  ', 1)
			same_day = a[0]
			print a[0], a[1].lstrip()
			dict.update({a[0] : a[1].lstrip()})
		else:
			same_day = c[0]
			dict.update({c[0] : c[1]})

I want a dictionary thats like this {'Thu Apr 04': 'Weigh In', 'Sat Apr 
13': '1:00pm  Get flag from dhariwal'...}

Saad


More information about the Tutor mailing list