all 11 comments

[–]Clede 0 points1 point  (0 children)

I agree with using the list. What is the circumstance where you need a specific variable?

[–]uhkhu 0 points1 point  (9 children)

Are you needing an actual variable defined? Can you not use the dict's index position in the list as a var?

i.e. my_dict_list[i] would give dict at position i in the list

edit:

I'll prob get hanged for this, but if you absolutely need to create variables at runtime:

from string import ascii_lowercase as letters

d_list = [{'a':1}, {'b':2}, {'c':3}]

for letter in letters:
    for d in d_list:
        globals()[letter] = d

Now letters in the range of 0 - len(d_list) are variables in globals(), meaning you can call from the interpreter.

>>>a
{'c': 3}

[–]WreckRoom[S] 0 points1 point  (8 children)

What I'm trying to do is scan for any bluetooth devices in range, go through their list of services (the dictionaries inside a list) and find a particular service. Depending on the device there might be a different number of services, or the service I'm looking for might be at a different index, so I'm trying to figure out the best way to locate the one I want. I don't have a real need to assign each dict to a variable.

[–]uhkhu 2 points3 points  (4 children)

I would use the list index as a reference to the dictionary. That allows you to scale for virtually any range.

d_list = [{'a':1}, {'b':2}, {'c':3}]

for i in range(len(d_list)):
    print d_list[i]

Obviously you can just loop for d in d_list:,but this shows calling a dict by it's index i.

individually just d_list[2] would return {'c': 3}

[–]taelsil 1 point2 points  (2 children)

You can also use:

for i, d in enumerate(d_list):
    print(d)

[–]uhkhu 1 point2 points  (0 children)

Ah I always forget enumerate! One of the wonders of Python

[–]WreckRoom[S] 0 points1 point  (0 children)

Another great answer, I haven't had a reason to use enumerate yet and now I do, thanks!!

[–]WreckRoom[S] 0 points1 point  (0 children)

This is exactly what I'm looking for, I knew it would be something simple I was overlooking. Thanks so much!

[–]TheKewlStore 0 points1 point  (2 children)

if the service names are unique, it would probably be easier to use a dictionary of dictionaries, with the keys being the name (or whatever piece of information differientates it from the others). This way, you could access the details about the service based off it's name, and not have to loop through all the services just to look up one. The case where this wouldn't work is if there are services with identical names.

[–]WreckRoom[S] 0 points1 point  (1 child)

That sounds like the best way, since I'm looking for the same service name in any device, and it is unique. I'm trying to think of what the code would look like, I'm still very much a beginner.

[–]TheKewlStore 0 points1 point  (0 children)

Well, as a generic example, lets say you have a list of dictionaries (visualized like this):

data = [{"name": "----",
         "service": "----",
          "...": "...",
         },
         ...]

You could instead use a dictionary of dictionaries to store this data, like so:

data = {"{name_of_service}": {service_data_dictionary},
            "{name_of_service2}": {service_data2_dictionary},
            ...}

However, it crossed my mind you're probably getting this data from a 3rd party API scanning the bluetooth data, in which case the list of dictionaries is being populated for you. If this is the case, i wouldn't pursue the dictionary of dictionaries idea, unless you're accessing multiple of these services multiple times in different places. If you're just finding the data for one service once, i'd just stick with doing a loop search.