list to dict

B

Bart Nessux

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

Thanks,
Bart
 
P

Peter Hansen

Bart said:
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
 
B

Bart Nessux

Peter said:
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...
 
J

Josef Dalcolmo

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
 
L

Larry Bates

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.
 
P

Peter Hansen

Bart said:
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
 
B

Bart Nessux

Josef said:
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
 
B

Bart Nessux

Larry said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top