list to dict

Discussion in 'Python' started by Bart Nessux, Jun 16, 2004.

  1. Bart Nessux

    Bart Nessux Guest

    What is the easiest/fastest way to build a dictionary from a list? The
    list contains 100,000 entries.

    Thanks,
    Bart
    Bart Nessux, Jun 16, 2004
    #1
    1. Advertising

  2. Bart Nessux

    Yermat Guest

    Bart Nessux wrote:
    > What is the easiest/fastest way to build a dictionary from a list? The
    > list contains 100,000 entries.
    >
    > Thanks,
    > Bart


    >>> dict([(1,'a'),(2,'b')])

    {1: 'a', 2: 'b'}

    list of tuples -> dict !

    --
    Yermat
    Yermat, Jun 16, 2004
    #2
    1. Advertising

  3. Bart Nessux

    Peter Hansen Guest

    Bart Nessux wrote:

    > What is the easiest/fastest way to build a dictionary from a list? The
    > list contains 100,000 entries.


    A dictionary has key/value pairs. How do you want to map
    the elements of your list to this format? In pairs, or
    are you using them all as keys, or something else?

    -Peter
    Peter Hansen, Jun 16, 2004
    #3
  4. Bart Nessux

    Bart Nessux Guest

    Peter Hansen wrote:
    > Bart Nessux wrote:
    >
    >> What is the easiest/fastest way to build a dictionary from a list? The
    >> list contains 100,000 entries.

    >
    >
    > A dictionary has key/value pairs. How do you want to map
    > the elements of your list to this format? In pairs, or
    > are you using them all as keys, or something else?
    >
    > -Peter


    1 = book1
    2 = book2
    3 = book3
    etc...
    Bart Nessux, Jun 16, 2004
    #4
  5. on Wed, 16 Jun 2004 09:29:23 -0400
    Bart Nessux <> wrote:

    > What is the easiest/fastest way to build a dictionary from a list? The
    > list contains 100,000 entries.


    The first question would be, what should the keys be? If the list consists of unique, unmutable items, then you might use the items themselves as key and write:

    mydict = dict(zip(mylist, mylist))

    obtaining a dictionary with all the keys and values identical (therefore somewhat of a waste, but occasionally useful). Note: if your original list did not contain unique values, you end up with a set.

    If you want to remember the original order of the list, then write

    mydict = dict(zip(mylist, xrange(len(mylist))))


    If you don't care about the key (that would be strange) then you can write:

    mydict = dict(zip(xrange(len(mylist)), mylist))

    Instead of len(mylist) you can also write 1000000 or any other number larger than your list.


    - Josef
    Josef Dalcolmo, Jun 16, 2004
    #5
  6. Bart Nessux

    Yermat Guest

    Bart Nessux wrote:
    > Peter Hansen wrote:
    >
    >> Bart Nessux wrote:
    >>
    >>> What is the easiest/fastest way to build a dictionary from a list?
    >>> The list contains 100,000 entries.

    >>
    >>
    >>
    >> A dictionary has key/value pairs. How do you want to map
    >> the elements of your list to this format? In pairs, or
    >> are you using them all as keys, or something else?
    >>
    >> -Peter

    >
    >
    > 1 = book1
    > 2 = book2
    > 3 = book3
    > etc...



    >>> dict(enumerate(['a','b','c']))

    {0: 'a', 1: 'b', 2: 'c'}
    >>>
    Yermat, Jun 16, 2004
    #6
  7. Bart Nessux

    Larry Bates Guest

    If the keys are just indexes that you would
    use in the dictionary, you don't need a
    dictionary at all. Just index into the list.

    list[0]->'book1'
    list[1]->'book2'
    list[2]->'book3'

    You will need to deal with the indexes beginning
    at zero (not 1) or put None in list[0] an then
    don't reference it.

    HTH,
    Larry Bates
    Syscon, Inc.

    "Bart Nessux" <> wrote in message
    news:capk64$8ku$...
    > Peter Hansen wrote:
    > > Bart Nessux wrote:
    > >
    > >> What is the easiest/fastest way to build a dictionary from a list? The
    > >> list contains 100,000 entries.

    > >
    > >
    > > A dictionary has key/value pairs. How do you want to map
    > > the elements of your list to this format? In pairs, or
    > > are you using them all as keys, or something else?
    > >
    > > -Peter

    >
    > 1 = book1
    > 2 = book2
    > 3 = book3
    > etc...
    Larry Bates, Jun 16, 2004
    #7
  8. Bart Nessux

    Peter Hansen Guest

    Bart Nessux wrote:

    > Peter Hansen wrote:
    >> A dictionary has key/value pairs. How do you want to map
    >> the elements of your list to this format? In pairs, or
    >> are you using them all as keys, or something else?

    >
    > 1 = book1
    > 2 = book2
    > 3 = book3
    > etc...


    This is much less clear than you might have thought, although
    with any luck Yermat and Larry have interpreted it correctly.

    Can you represent the list as you would in Python? Then
    we'll know exactly what you are talking about. Or did you
    not even mean that you had a Python list, but merely a "list"
    of things, maybe in a file?

    The above could be interpreted at least these ways:

    1. lines in a file
    2. [1, book1, 2, book2, 3, book3]
    3. [book1, book2, book3]
    4. ['book1', 'book2', 'book3']
    5. ['1 = book1', '2 = book2', '3 = book3']
    and probably many others.

    -Peter
    Peter Hansen, Jun 16, 2004
    #8
  9. Bart Nessux

    Bart Nessux Guest

    Josef Dalcolmo wrote:
    > on Wed, 16 Jun 2004 09:29:23 -0400
    > Bart Nessux <> wrote:
    >
    >
    >>What is the easiest/fastest way to build a dictionary from a list? The
    >>list contains 100,000 entries.

    >
    >
    > The first question would be, what should the keys be? If the list consists of unique, unmutable items, then you might use the items themselves as key and write:
    >
    > mydict = dict(zip(mylist, mylist))
    >
    > obtaining a dictionary with all the keys and values identical (therefore somewhat of a waste, but occasionally useful). Note: if your original list did not contain unique values, you end up with a set.
    >
    > If you want to remember the original order of the list, then write
    >
    > mydict = dict(zip(mylist, xrange(len(mylist))))
    >
    >
    > If you don't care about the key (that would be strange) then you can write:
    >
    > mydict = dict(zip(xrange(len(mylist)), mylist))
    >
    > Instead of len(mylist) you can also write 1000000 or any other number larger than your list.
    >
    >
    > - Josef


    Josef,

    This was great. I understand how this works now. Thanks for such a
    broad, yet applicable explanation!

    Bart
    Bart Nessux, Jun 16, 2004
    #9
  10. Bart Nessux

    Bart Nessux Guest

    Larry Bates wrote:
    > If the keys are just indexes that you would
    > use in the dictionary, you don't need a
    > dictionary at all. Just index into the list.
    >
    > list[0]->'book1'
    > list[1]->'book2'
    > list[2]->'book3'
    >
    > You will need to deal with the indexes beginning
    > at zero (not 1) or put None in list[0] an then
    > don't reference it.
    >
    > HTH,
    > Larry Bates
    > Syscon, Inc.


    Thank you Larry, this is fascinating. From your post, I now understand
    that a dictionary is much like a database.

    For my purpose, I like the idea of an indexed list (this too reminds me
    of a database somewhat). Thank you for helping me to understand this. I
    can see other objects such as a phone book... I think a dictionary would
    be more appropriate for that as it's not really just an indexed list,
    but more of a database that associates a number (that has meaning and
    purpose) to a name.

    Bart
    Bart Nessux, Jun 16, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    402
    Skip Montanaro
    Aug 15, 2003
  2. Alexander Kozlovsky

    dict!ident as equivalent of dict["ident"]

    Alexander Kozlovsky, May 21, 2006, in forum: Python
    Replies:
    5
    Views:
    345
    Alexander Kozlovsky
    May 22, 2006
  3. Paul Melis

    dict.has_key(x) versus 'x in dict'

    Paul Melis, Dec 6, 2006, in forum: Python
    Replies:
    48
    Views:
    1,296
    Kent Johnson
    Dec 15, 2006
  4. Almad
    Replies:
    8
    Views:
    386
    Terry Reedy
    Dec 14, 2006
  5. Drew
    Replies:
    19
    Views:
    1,327
    Duncan Booth
    Mar 15, 2007
Loading...

Share This Page