What I am wondering is how I can loop through paired_devices and get the "name" attribute for each device. This is only a short amount of the code, if more is needed, feel free to comment below.

 def parse_device_info(self, info_string):
    attribute_list = info_string[device_position:].split(" ", 2)
                            device = {
                                "mac_address": attribute_list[1],
                                "name": attribute_list[2]
     return device

if__name__ == "__main__":

#Bluetoothctl is the class that has the get_paired_devices() method
bl = Bluetoothctl()
#get_paired_devices() returns an array of devices, using parse_device_info()
paired_devices = bl.get_paired_devices() 
  • Please indent you code correctly. Also, if you show what the data you are trying to iterate on looks like, and what you are trying to extract would look like, you are more likely to get a good answer. – Stephen Rauch Oct 7 '17 at 14:34

From your comments in the code, i see that paired_devices is a list of instances of the "device" class. So if you want to loop through each device contained in the array, and access the "name" attribute something like this should work:

devices_names = []

for a_device in paired_devices:

You could stored these names in another list as above or do anything else you require, that's just an example to show how to access the name attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.