0

I have a List containing Three Dictionaries as below:

lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}

alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}

tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}

students = [lloyd, alice, tyler]

I want to loop through the list and print only the names of dictionaries

i.e.

lloyd
alice
tyler

I do not want to print the keys or values of the dictionaries themselves.

Any suggestions in this regard will be helpful

karthikilan
  • 27
  • 2
  • 4
  • If the names of the dictionaries are same as the attribute `name`, why not print that instead? – Quill Aug 22 '15 at 17:03
  • I don't think this is possible in Python, and if it is it's not good practice. What exactly are you trying to do? This has nothing to with dictionaries. – veggie1 Aug 22 '15 at 17:06
  • 2
    See here: http://stackoverflow.com/questions/1538342/how-can-i-get-the-name-of-an-object-in-python – fukanchik Aug 22 '15 at 17:10

5 Answers5

2

Objects in Python don't "have names", they are bound to variables. Consider the following code:

lst1 = [3, [4, 5, 6], 7]
lst2 = lst1

What's the "name" of the list (it can be accessed either as lst1 or lst2). What's the name of the second element of the list (only the list stores a reference to the sub-list).

The dir() function can give you a list of the names in a namespace, but that's about the best you can do.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
2

I know this is old but I believe I have a solution to the question asked. This can be accomplished by nesting these dictionaries into another dictionary.

Take the original code:

lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}

alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}

tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}

students = [lloyd, alice, tyler]

Instead of using the list of dictionaries, create a nested dictionary called users with the dictionaries of the users as members.

users = {

    'lloyd' : {
        "name": "Lloyd",
        "homework": [90.0, 97.0, 75.0, 92.0],
        "quizzes": [88.0, 40.0, 94.0],
        "tests": [75.0, 90.0]
        },

    'alice' : {
        "name": "Alice",
        "homework": [100.0, 92.0, 98.0, 100.0],
        "quizzes": [82.0, 83.0, 91.0],
        "tests": [89.0, 97.0]
        },

    'tyler' : {
        "name": "Tyler",
        "homework": [0.0, 87.0, 75.0, 22.0],
        "quizzes": [0.0, 75.0, 78.0],
        "tests": [100.0, 100.0]
        }
    }

Then a simple for loop to print the username...

for username, user_info in users.items():
    print(f"{username.title()}")

This gives the desired output..

Lloyd
Alice
Tyler

I am just learning myself so I hope this helps someone like the stack overflow community has helped me!

gto_gary
  • 21
  • 2
1
for i in students:print(i['name'])
Hybrid
  • 321
  • 3
  • 13
1
for x in dict.items():
    print(x[0])
Sean
  • 11
  • 1
  • 2
    Welcome to Stack Overflow. It would be great if you would describe your code and state how it will solve the problem. – Patrick Klein Nov 13 '20 at 17:33
0

there is not __name__ or such attr in standard dict class.(functions have func.func_name) I am sure there would be some better implementations but my solution would be like below:

create a new type of dict with some additional attributes ..

class myspecial_dict(dict):
    def __init__(self,Name,**kwargs):
        super(myspecial_dict,self).__init__(kwargs)
        self.dictname=Name

mys=myspecial_dict("lloyd",**lloyd)
print mys.dictname
print mys['quizzes']

that prints out:

lloyd
[88.0, 40.0, 94.0]

if you want to do this for all:

dicts    =[lloyd,alice,tyler]
dictnames=["lloyd","alice","tyler"]
print [myspecial_dict(x,**y).dictname for x,y in zip(dictnames,dicts) ]

that yields:

['lloyd', 'alice', 'tyler']
Krcn U
  • 411
  • 1
  • 9
  • 16