简体   繁体   中英

Creating list of dictionary not working

I am trying to create an array of hash in python but it is not working

data = ["long","short","fanouts"]
app = []

for da in data:
    app.append(app[name] = da)

output

File "test.py", line 5
    app.append(app[name] = da)
SyntaxError: keyword can't be an expression

Please could anyone help me with correct code i am new to python

When you write

abc(x=y)

the interpreter reads that as trying to call a function with a keyword argument. So reading your line

app.append(app[name] = da)

it thinks you have a keyword argument app[name] , which does not make sense as a keyword argument.

If you want to append a dict to your list, you could do it like this:

app.append({name:da})

as long as name and da are existing variables.

Try this:

data = ["long","short","fanouts"]
app = []

for da in data:
    app.append({name: da})

Depends on what you want app[name] to be (assuming app is a dict). Either

app[name].append(da)

or

app.update(name=da)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM