You are viewing a single comment's thread from:

RE: Python Libraries: The Random Splash Voter

in STEMGeeks2 years ago (edited)

I still don’t understand why the (type(voterlist)) command returns a list

I guess it is "active_votes": ["trayciyork","slobberchops","steevc"], hence voterlist=["trayciyork","slobberchops","steevc"], which naturally returns a list as a type

Had to edit the comment to add a suggestion to deal with duplicate votes - to keep a list of items voted on. So if true, vote, add to the list, check if on the list if not, check if true, vote, add to the list...
Just an idea

Sort:  

If you print what (voterlist) returns.. this is what it looks like:

image.png

Those pairings are the hallmarks of a dictionary variable, and yet it insists it's a list.

Since it's [], it's a list.

From what I can see, [] is a list, and {} is a dictionary, so [{}], .. is as @rimicane says, a list of dictionaries.

image.png
Dictionary

image.png
List

It seems there is a such a thing, which I wasn't aware of. The regular 'in' operator as a search does not work on this type so I just messed around with it until I found something that did!

https://pythonexamples.org/python-list-of-dictionaries/

It's a list of JSONs, actually.

for thing in list:
    if thing['voter'] == 'thingey':
        do stuff

Something like this should work.

Yes, that's exactly what I used.

 for item in voterlist:
     if item['voter'] == strvoter:

ok, still a list type ( or better say a list of dictionaries):

[ { }, { }, { } ... { } ]