Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem of converting a list to dict

Reply
Thread Tools

problem of converting a list to dict

 
 
Louis.Soninhu@gmail.com
Guest
Posts: n/a
 
      01-09-2008
Hi pals

I have a list like this

mylist=['','tom=boss','mike=manager','paul=employee','mean ingless']

I'd like to remove the first and the last item as they are irrevalent,
and convert it to the dict:
{'tom':'boss','mike':'manager','paul':'employee'}

I tried this but it didn't work:

mydict={}
for i in mylist[1:-1]:
a=i.split('=') # this will disect each item of mylist into a 2-item
list
mydict[a[0]]=a[1]

and I got this:
File "srch", line 19, in <module>
grab("a/tags1")
File "srch", line 15, in grab
mydict[mylist[0]]=mylist[1]
IndexError: list index out of range

Anyone could shed me a light on this?

thanks
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-09-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I have a list like this
>
> mylist=['','tom=boss','mike=manager','paul=employee','mean ingless']
>
> I'd like to remove the first and the last item as they are irrevalent,
> and convert it to the dict:
> {'tom':'boss','mike':'manager','paul':'employee'}
>
> I tried this but it didn't work:
>
> mydict={}
> for i in mylist[1:-1]:
> a=i.split('=')
> mydict[a[0]]=a[1]
>
> and I got this:
> File "srch", line 19, in <module>
> grab("a/tags1")
> File "srch", line 15, in grab
> mydict[mylist[0]]=mylist[1]
> IndexError: list index out of range
>
> Anyone could shed me a light on this?


works for me, with the mylist example you provided.

to see what's going on on your machine, try printing "a" after the
split, but before you use it to populate the dictionary.

</F>

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      01-09-2008
On Wed, 09 Jan 2008 10:56:36 -0800, Louis.Soninhu wrote:

> Hi pals
>
> I have a list like this
>
> mylist=['','tom=boss','mike=manager','paul=employee','mean ingless']
>
> I'd like to remove the first and the last item as they are irrevalent,
> and convert it to the dict:
> {'tom':'boss','mike':'manager','paul':'employee'}
>
> I tried this but it didn't work:
>
> mydict={}
> for i in mylist[1:-1]:
> a=i.split('=') # this will disect each item of mylist into a 2-item
> list
> mydict[a[0]]=a[1]
>
> and I got this:
> File "srch", line 19, in <module>
> grab("a/tags1")
> File "srch", line 15, in grab
> mydict[mylist[0]]=mylist[1]
> IndexError: list index out of range
>
> Anyone could shed me a light on this?


The real list you used had at least one string without a '=' in it. The
list given above doesn't raise that exception:

In [102]: mylist=['','tom=boss','mike=manager','paul=employee','mean ingless']

In [103]: mydict={}

In [104]: for i in mylist[1:-1]:
.....: a=i.split('=')
.....: mydict[a[0]]=a[1]
.....:

In [105]: mydict
Out[105]: {'mike': 'manager', 'paul': 'employee', 'tom': 'boss'}

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Louis.Soninhu@gmail.com
Guest
Posts: n/a
 
      01-09-2008
that's very strange...

the list I give here is almost same as the real list, except for the
length.

Thanks Marc, I'll go check what's wrong elsewhere
 
Reply With Quote
 
Louis.Soninhu@gmail.com
Guest
Posts: n/a
 
      01-09-2008
On Jan 9, 3:05*pm, Fredrik Lundh <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > I have a list like this

>
> > mylist=['','tom=boss','mike=manager','paul=employee','mean ingless']

>
> > I'd like to remove the first and the last item as they are irrevalent,
> > and convert it to the dict:
> > {'tom':'boss','mike':'manager','paul':'employee'}

>
> > I tried this but it didn't work:

>
> > mydict={}
> > for i in mylist[1:-1]:
> > * *a=i.split('=')
> > * *mydict[a[0]]=a[1]

>
> > and I got this:
> > * File "srch", line 19, in <module>
> > * * grab("a/tags1")
> > * File "srch", line 15, in grab
> > * * mydict[mylist[0]]=mylist[1]
> > IndexError: list index out of range

>
> > Anyone could shed me a light on this?

>
> works for me, with the mylist example you provided.
>
> to see what's going on on your machine, try printing "a" after the
> split, but before you use it to populate the dictionary.
>
> </F>- Hide quoted text -
>
> - Show quoted text -


'print a' works
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-09-2008
(E-Mail Removed) wrote:

>> to see what's going on on your machine, try printing "a" after the
>> split, but before you use it to populate the dictionary.

>
> 'print a' works


so what does it tell you?

</F>

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-09-2008
On Jan 10, 5:56 am, (E-Mail Removed) wrote:
> Hi pals
>
> I have a list like this
>
> mylist=['','tom=boss','mike=manager','paul=employee','mean ingless']
>
> I'd like to remove the first and the last item as they are irrevalent,
> and convert it to the dict:
> {'tom':'boss','mike':'manager','paul':'employee'}
>
> I tried this but it didn't work:
>
> mydict={}
> for i in mylist[1:-1]:
> a=i.split('=') # this will disect each item of mylist into a 2-item


No it doesn't; it dissects i into a 2-item list if i is a string
containing exactly one '='.

DON'T rely on "knowing" that the first and last entries are the only
irrelevant ones. Do some checking. Conditions to check for:
(1) len(a) == 2
(2) a[0] is empty or not what you expect (a person's name)
(3) a[1] is empty or not what you expect (a job title)
(consider what happens with 'tom = boss' ... a[0] = 'tom ', a[1] = '
boss')
(4) duplicate keys [...., 'tom=boss', 'tom=clerk', ...]


 
Reply With Quote
 
Reedick, Andrew
Guest
Posts: n/a
 
      01-09-2008
> -----Original Message-----
> From: python-list-bounces+jr9445=(E-Mail Removed) [mailtoython-
> list-bounces+jr9445=(E-Mail Removed)] On Behalf Of Fredrik Lundh
> Sent: Wednesday, January 09, 2008 2:39 PM
> To: (E-Mail Removed)
> Subject: Re: problem of converting a list to dict
>
> (E-Mail Removed) wrote:
>
> >> to see what's going on on your machine, try printing "a" after the
> >> split, but before you use it to populate the dictionary.

> >
> > 'print a' works

>
> so what does it tell you?
>


A bigger hint:
a=i.split('=')
print "'%s' splits into " % (i), a
assert len(a) == 2
mydict[a[0]]=a[1]


 
Reply With Quote
 
Louis.Soninhu@gmail.com
Guest
Posts: n/a
 
      01-09-2008
oops, it seems there are other 'meaningless' item, which actually
caused the problem

Thanks for helps
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-09-2008
On Jan 10, 6:52 am, "Reedick, Andrew" <(E-Mail Removed)> wrote:
> > -----Original Message-----
> > From: python-list-bounces+jr9445=(E-Mail Removed) [mailtoython-
> > list-bounces+jr9445=(E-Mail Removed)] On Behalf Of Fredrik Lundh
> > Sent: Wednesday, January 09, 2008 2:39 PM
> > To: (E-Mail Removed)
> > Subject: Re: problem of converting a list to dict

>
> > (E-Mail Removed) wrote:

>
> > >> to see what's going on on your machine, try printing "a" after the
> > >> split, but before you use it to populate the dictionary.

>
> > > 'print a' works

>
> > so what does it tell you?

>
> A bigger hint:
> a=i.split('=')
> print "'%s' splits into " % (i), a


consider:
(1) using %r instead of '%s'
(2) omitting the redundant space after 'into'
(3) losing the redundant () around i


> assert len(a) == 2
> mydict[a[0]]=a[1]


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dict.items() vs dict.iteritems and similar questions Drew Python 19 03-15-2007 09:23 PM
dict.has_key(x) versus 'x in dict' Paul Melis Python 48 12-15-2006 05:55 PM
Inconsistency in dictionary behaviour: dict(dict) not calling __setitem__ Almad Python 8 12-14-2006 07:37 PM
dict!ident as equivalent of dict["ident"] Alexander Kozlovsky Python 5 05-22-2006 08:06 AM
Re: dict->XML->dict? Or, passing small hashes through text? Skip Montanaro Python 0 08-15-2003 03:46 PM



Advertisments