RE: Python Libraries: The Random Splash Voter

avatar
(Edited)

You are viewing a single comment's thread:

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



0
0
0.000
7 comments
avatar

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.

0
0
0.000
avatar

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

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

0
0
0.000
avatar

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

0
0
0.000
avatar

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/

0
0
0.000
avatar
(Edited)

It's a list of JSONs, actually.

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

Something like this should work.

0
0
0.000
avatar

Yes, that's exactly what I used.

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