python: creating a list inside a dictionary

Posted by user1871081 on Stack Overflow See other posts from Stack Overflow or by user1871081
Published on 2012-12-02T22:52:53Z Indexed on 2012/12/02 23:03 UTC
Read the original article Hit count: 131

Filed under:
|
|

I just started using python and I'm trying to create a program that will read a file that looks like this:

AAA x 111
AAB x 111
AAA x 112
AAC x 123
...

the file is 50 lines long and I'm trying to make the letters into keys in a dictionary and the numbers lists that correspond with the keys.

I want the output to look like this:

{AAA: ['111', '112'], AAB: ['111'], AAC: [123], ...}

This is what I've tried

file = open("filename.txt", "r") 
readline = file.readline().rstrip()
while readline!= "":
    list = []
    list = readline.split(" ")
    j = list.index("x")
    k = list[0:j]
    v = list[p + 1:]
    d = {}
    if k in d == False
        d[k] = []
        d[k].append(v)
    else
        d[k].append(v)
    readline = file.readline().rstrip()

I keep getting syntax errors on my if statement and I can't figure out what I've done wrong.

© Stack Overflow or respective owner

Related posts about python

Related posts about list