468,110 Members | 1,797 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

list to dict

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

Thanks,
Bart
Jul 18 '05 #1
9 2332
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

Jul 18 '05 #2
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
Jul 18 '05 #3
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...
Jul 18 '05 #4
on Wed, 16 Jun 2004 09:29:23 -0400
Bart Nessux <ba*********@hotmail.com> 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
Jul 18 '05 #5
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'}


Jul 18 '05 #6
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" <ba*********@hotmail.com> wrote in message
news:ca**********@solaris.cc.vt.edu...
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...

Jul 18 '05 #7
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
Jul 18 '05 #8
Josef Dalcolmo wrote:
on Wed, 16 Jun 2004 09:29:23 -0400
Bart Nessux <ba*********@hotmail.com> 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
Jul 18 '05 #9
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
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by David Bear | last post: by
30 posts views Thread by Steven Bethard | last post: by
90 posts views Thread by Christoph Zwerschke | last post: by
35 posts views Thread by erikwickstrom | last post: by
20 posts views Thread by Seongsu Lee | last post: by
27 posts views Thread by Mark | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.