[Tutor] how to iterate through a dictionary and assign list values?

Danny Yoo dyoo at hashcollision.org
Thu Dec 5 06:48:04 CET 2013


On Wed, Dec 4, 2013 at 8:13 PM, Jared Nielsen <nielsen.jared at gmail.com>wrote:

> I want to create a dictionary, assign it keys, then iterate through a for
> loop and assign the dictionary values from a list. I'm trying this, but
> it's not working:
>
> dictionary = {"one", "two", "three"}
> list = [1,2,3]
>
>
Hi Jared,

'dictionary' here is misnamed in the program: it's not a dictionary that
you've constructed here, but a 'set'.  Sets and dictionaries in Python
unfortunately use very similar syntax, which leads to this confusion.

Try:

    dictionary = {}

Start with that.  You can then say things like:

    dictionary["one"] = 1

Read:

    http://docs.python.org/3.3/tutorial/datastructures.html#dictionaries

for a few more examples with dictionaries.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131204/780ddd17/attachment.html>


More information about the Tutor mailing list