Python Forum
AttributeError: 'list' object has no attribute 'values'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'list' object has no attribute 'values'
#1
I have a query. My query returns the result that i want as a dictionary.

Like:

[{'community_string': 'public'}, {'community_string': 'private'}]
I want to use only public and private as a string. For doing it, i wrote my code like that:

query= "SELECT community_string FROM nat_snmp_string WHERE status ='enable'"
cursor = connection.cursor(dictionary=True)
cursor.execute(query)
com_str = cursor.fetchall()
com_str_string = com_str.values()
cmnyt_str = json.dumps(list(com_str))
print(cmnyt_str)  
cmd_line = cmnyt_str.replace(':','')
cmd_line = cmd_line.replace('"','')
cmd_line = cmd_line.replace(']','')
cmd_line = cmd_line.replace('[','')
  
but it gives me AttributeError: 'list' object has no attribute 'values' error.
How can i solve this issue?
Thank you for your helps.
Reply
#2
The error is from trying to access a list like a dict.

you can use the list index to access the data or loop the list to get the data.

com_str = [{'community_string': 'public'}, {'community_string': 'private'}]

print('list index')
print(f"{com_str[0]['community_string']} | {com_str[1]['community_string']}")

print()
print('looping')
for data in com_str:
    print(data['community_string'])

print()
print('One liner')
print('\n'.join([data['community_string'] for data in com_str]))
Output:
list index public | private looping public private One liner public private
ilknurg likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Thank you for your answer. But in my db, there may be more than 2 attribute. In this case i cant write all like that:

com_str = [{'community_string': 'public'}, {'community_string': 'private'}]
 
print(f"{com_str[0]['community_string']} |
{com_str[1]['community_string']}")
Reply
#4
(Jan-19-2022, 08:25 AM)menator01 Wrote: The error is from trying to access a list like a dict.

you can use the list index to access the data or loop the list to get the data.

com_str = [{'community_string': 'public'}, {'community_string': 'private'}]

print('list index')
print(f"{com_str[0]['community_string']} | {com_str[1]['community_string']}")

print()
print('looping')
for data in com_str:
    print(data['community_string'])

print()
print('One liner')
print('\n'.join([data['community_string'] for data in com_str]))
Output:
list index public | private looping public private One liner public private

Thank you very much!
Reply
#5
Then you will need to use some type of loop
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to capitalize letter in list object Python iwonkawa 4 366 May-29-2024, 04:29 PM
Last Post: DeaD_EyE
  remove duplicates from dicts with list values wardancer84 27 1,507 May-27-2024, 04:54 PM
Last Post: wardancer84
  Printing out incidence values for Class Object SquderDragon 3 547 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  Copying the order of another list with identical values gohanhango 7 1,474 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,505 Nov-03-2023, 05:35 PM
Last Post: huzzug
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 1,100 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 2,476 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,973 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 947 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 4,764 Jun-18-2023, 07:47 PM
Last Post: Melcu54

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020