tags:

views:

84

answers:

4

So this is the situation: I have 2 lists and want to put them in a dictionary.

Content ['This is Sams Content', 'This is someone's else content']
Author ['Sam', 'Someone Else']

This is the dictionary I would like to create

Reviews [{'content': 'This is Sams Content', 'author' : 'Sam'} , {'content': 'This is someone's else content', 'author' : 'Someone Else'}

I hope you understand what the question is. Thanks for helping.

A: 
content = ['This is Sams Content', 'This is someone\'s else content'] 
author = ['Sam', 'Someone Else']

reviews = []

for i in range(len(author)):
    d = {
        'content': content[i],
        'author': author[i]
    }
    reviews.append(d)

for r in reviews:
    print "Author: %s, content: %s" % (r['author'], r['content'])
Simon Whitaker
Thanks really clear answer. Should have been able to figure this out with the documentation, but I always find it a little hard to read the python documentation. It seems not so well structured.
Sam S
Glad it helped Sam! The docs get easier to read the more you read them and the more Python you write, so stick with it! :)
Simon Whitaker
`range(len(...))` is not python - use `enumerate(...)`. But the bettere solution for the question is to use `zip`.
PreludeAndFugue
It looks like python to me. :) I think you mean that it's not idiomatic python, which is fair enough - but it works and it's easy to understand
Simon Whitaker
+7  A: 

You're looking for zip I believe. Something like this:

reviews = [{'content': c, 'author': a} for c, a in zip(contentList, authorList)]
g.d.d.c
Nice! I hadn't seen zip() before. :)
Simon Whitaker
It's essential. It aids in this type of situation to no end.
g.d.d.c
hehe even better than Simon's answer although Simon's answer is more readable for python newbs like me. Gotta read the zip documentation.
Sam S
@Sam - At first list comprehensions look a little bit complex, but once you work with them for a little bit you'll never want to go back to a language without them.
g.d.d.c
Yea I know. I use them quite a lot now too for simple operations with lists and dictionaries. I really like them ;)
Sam S
A: 
reviews = []
authors = ['sam', 'dave']
content = ['content by sam', 'content by dave']
for a, c in zip(authors, content):
    reviews.append({'content':c, 'author':a})
print reviews
dave paola
g.d.d.c.'s answer is more concise and pythonic
dave paola
A: 

Assuming Content and Author are arrays as defined in the question, and assuming you want a single resulting dict:

d = {}

for i in range(len(Content)):
   d[Content[i]] = Author[i]
dls
`for i in range(len(x))` is a no-go in python unless you're really only juggling with indices. -1
delnan
@delnan: can you provide some documentation to support this? I'm assuming you're talking about it being non-pythonic, which is fine, but it **does** work :)
dls
Documentation? Just ask *any* experienced python coder... just look at the code samples in *any* python book/tutorial with good rep... yeah, it "works", but it is inferior to the idiomatic alternative in **any** way.
delnan
@dls, if you need to iterate through something while keeping an index counter, use `for i, x in enumerate(Content)`.
Daniel Roseman