• Register
1 vote
3.7k views

Problem:

I have a dictionary with some users' data in it. Trying to use the dictionary to call with .append() function but it throws an error that says attributeerror: 'list' object has no attribute 'keys'

But I haven’t declared it as a list in my program. Where am I doing wrong? Could you please fix it? I am attaching my code snippet below:

users = [{'id':101,'name':"Admin",'type':1},{'id':102,'name':"Admin2",'type':1}]
def myData(dict, entry):
new = {}
n = 0
for i in dict.keys():
    new[i] = entry(n)
    n += 1
dict.append(new)

Many thanks for your help.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

Correction please, that is not a dictionary but the lists of a dictionary. Why don’t you declare a dictionary like this?

users = [{'id':101,'name':"Admin",'type':1}' {'id':102,'name':"Admin2",'type':1}]

newusers = dict()

for ud in users:
    newusers[ud.pop('id')] = ud
print newusers
newusers[102] = {'name': 'Roy', 'type': 2}
print newusers

Now, this should work fine.

13 9 6
94,260 points

Related questions

0 votes
1 answer 26 views
26 views
Problem: please help me to fix this : Attributeerror: 'list' object has no attribute 'keys'.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 6 views
6 views
Problem: Hello! I am a new programmer and I recently learned the concept of container types in Python. I have never worked with dictionaries and lists before. I tried running the following program in which I am adding the items into a dictionary through a list: lst = ... but I am unable to understand why it is showing an error with the dictionary. Could someone please help me sort this error out?
asked Apr 6 Code Learner 9.5k points
0 votes
1 answer 114 views
114 views
Problem: I have a small problem with list. So i have a list called l: l = ['Facebook;Google+;MySpace', 'Apple;Android'] And as you can see I have only 2 strings in my list. I want to separate my list l by ';' and put my new 5 strings into a new list called ... tried to do this like this: l1 = l.strip().split(';') But Python give me an error: AttributeError: 'list' object has no attribute 'strip'
asked Feb 13 Mashhoodch 13k points
0 votes
0 answers 536 views
536 views
function anagrams(s1, s2) is a Boolean valued function, which returns true just in case the string s1 contains the same letters as string s2 but in a different order. The function should be case insensitive --- in other words it should return the same value if any letters in either s1 or ... ; Ayxan Haqverdili Nov 12 '18 at 15:43 I've no idea..... maybe my logic is wrong, the output is so wired...
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 6 views
6 views
Problem: My friend's computer showing this error by two to three days,he tried to fix but he can't able to, then he requested me to help but i am a beginner in this field then i think i have to post here my problem by which i can got my answer, can anyone help me out?
asked Apr 4 niyatiry 3.6k points
0 votes
1 answer 3 views
3 views
Problem: I face trouble to come out from this, if anyone know what i have to do then please answer me.
asked Apr 1 niyatiry 3.6k points
0 votes
1 answer 6 views
6 views
Problem: If anyone know about this then please answer me. I can't able to solved this problem by myself, that's why I'm posting here. If anyone know about this then please answer me.
asked Apr 1 niyatiry 3.6k points
0 votes
2 answers 13 views
13 views
Problem: how to I Solve this: error: attributeerror: 'module' object has no attribute 'create_default_context' HELP! I need help understanding this: attributeerror: 'module' object has no attribute 'create_default_context' Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 11 Mashhoodch 13k points
0 votes
1 answer 7 views
7 views
Problem: how to I Solve this: attributeerror: 'nonetype' object has no attribute 'encode' HELP! I need help understanding this: attributeerror: 'nonetype' object has no attribute 'encode' Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 9 Mashhoodch 13k points
0 votes
1 answer 104 views
104 views
Problem: How to I Solve this: attributeerror: 'function' object has no attribute HELP! I need help understanding this: attributeerror: 'function' object has no attribute Can someone please help solve this, I am new in python().
asked Mar 2 Mashhoodch 13k points