deounix
deounix

Reputation: 141

Add an item to the list with Python

I'm trying to add new item to list using .append() but not work with my issue I want to insert data into list like this :

hello = {}

why like this? I explained it with The code

hello = {} # I don't want to use with hello : []
# Because I'll use it to write into file like this : {u'list1': {'hello1': {}, 'hello2': {}}, u'list2': {'hello3': {}, 'hello4': {}}}

GoodWords = ["hello1", "hello2"]

def Write(filename, data):
    fp = file(filename, 'w')
    fp.write(data)
    fp.close()

"""
Here why I would like to use {} not []
It'll be write like this : {u'list1': {'hello1': {}, 'hello2': {}}}

def start():
    if not "list1" in hello:
        hello["list1"] = {}
    for x in GoodWords:
        hello["list1"][x] = {}
    Write("test.txt", str(hello))


"""

def start():
    # Any idea to use .append() with {} or something like
    for x in GoodWords:
        hello.append(x)

try: start(); sleep(4)
except Exception as why: print why

thanks.

Upvotes: 0

Views: 127

Answers (1)

doptimusprime
doptimusprime

Reputation: 9395

This hello is a dictionary, not a list.

Either use

hello = []

or

hello[x] = "something"

Upvotes: 2

Related Questions