1


I am currently writing a small bit of logic for my HTML page. My aim is to create variables (lists) within an iteration (using the iteration to create the names of said lists as the amount of them will be unknown to the program). I am currently creating the lists like this:

maps={}
    currentMap = elements[0].process
    counter=0
    for i in elements:
        if(counter==0):
            maps["mapsEle{0}".format(counter)]=[]
            counter+=1
        if(i.process!=currentMap):
            currentMap = i.process
            maps["mapEle{0}".format(counter)]=[]
            counter+=1
        else:
            print("No change found, keeping heading the same.")

However as you can probably tell, this does not create a list but a string. I try to print the variables (e.g. mapsEle0) and it returns the variable name (e.g. print(mapsEle0) returns "mapsEle0") this too me is suprising as I would have thought if the dictionary is saving it as a string it would print "[]".

So I am looking for a way to create lists within the dictionary in that iteration I am using there, basically want to just reformat my declaration. Cheers in advance everyone :)

Edit:
As requested here is the code where I attempt to append. Please note I want to append 'i' into the lists and no the dictionary.

for i in maps:
        for x in elements:
            if(x.process!=currentMap):
                currentMap=x.process
            elif(x.process==currentMap):
                #i.append(x)
1
  • have an empty list initialized before for loop and append 'i' to it Commented Nov 11, 2016 at 12:14

2 Answers 2

1

The syntax of your print statement is wrong, if you want to access the contents of the dictionary, you need to use different notation.

Instead of print('mapsEle0') you need to do print(maps['mapsEle0']).

Update:

Unfortunately your description of what you want and your code are a bit conflicting, so if you can, please try to explain some more what this code is supposed to do.

for i in maps.iterkeys():
        for x in elements:
            if(x.process!=currentMap):
                currentMap=x.process
            elif(x.process==currentMap):
                maps[i].append(x)

This will iterate over all keys of maps ('mapsEle0'...'mapsEleN') and add x to the contained list if the elif condition is fulfilled.

Sign up to request clarification or add additional context in comments.

7 Comments

Okay, thank you that makes sense however that is not the main issue, if you read the question the issue is that mapsEle0 is a string and not a list. I need to append into the list and it gives "str has no attribute append"
@J.Scull I suspect there's an error in your append syntax too. Please add that code to your question. I cannot help you fix code that isn't there. The way you create the empty lists as values in the dictionary looks fine.
Thank you the print worked. I have added the append code into the question.
@J.Scull See my edit. This will iterate over all elements of maps and append x to the corresponding list. Is this what you want? Your description is a bit ambiguous, unfortunately.
So what I want that to do is loop through all of the elements contained in maps, which currently lets say hold 3 lists; mapsEle0, mapsEle1 and mapsEle2. The first if reads the object I am trying to store in the lists and stores them in respective lists according to their process. So mapsEle0 hold all elements with the process "inputs" for example. When the iteration detects a new heading (not matching the heading of the previous element) it moves onto the next list and starts storing those elements in that list further categorising them. I hope that makes it clearer.
|
1

You're printing the string by doing print('mapsEle0'). To print the dict, you must print(maps) - 'll print the whole dictionary, OR, to print a specific key/element print(maps['mapsEle0'])

To elaborate it further here's a interpreter session:

>>> maps = {}
>>> counter = 0
>>> maps["mapsEle{0}".format(counter)]=[]
>>> maps 
{'mapsEle0': []}
>>>
>>> print(maps)
{'mapsEle0': []}
>>>
>>> print(maps['mapsEle0'])
[]
>>> 

For the append part:

>>> maps['mapsEle1'].append('hello')
>>> print(maps['mapsEle1'])
['hello']

Edit 2: Your statement is still not clear

As requested here is the code where I attempt to append. Please note I want to append 'i' into the lists and no the dictionary.

I think sobek has got it right - you want to append x to the mapsEle0, mapsEle1 lists, which are keys in maps dictionary.

for i in maps.iterkeys():
    for x in elements:
        if(x.process!=currentMap):
             currentMap=x.process
        elif(x.process==currentMap):
             maps[i].append(x)

4 Comments

I have added the append code into the question. The issue being that I need to append in a loop of unknown length so I cannot specify the lists manually/one by one.
Please note that i missed the empty parentheses after maps.iterkeys
@Nabeel Ahmed I have commented on sobeks post with what I hope to be a more clear description of my goal
I just noticed I wrote I wanted to append i into the list, which as you said is wrong, I want to append x (element) into the list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.