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
  Copying the order of another list with identical values gohanhango 7 1,060 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 718 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,528 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,385 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 704 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,662 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 685 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  Comparing List values to get indexes Edward_ 7 1,082 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,210 Apr-15-2023, 05:17 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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