Python Forum

Full Version: making list in looping a dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how to make a list out of this ?
is this the right approach?
users={"glenn":1,"elena":2,"mama":3}
names=(list(users.keys()))
for i in names:
    # i should become a list of each 3 keys in a dictionay

    print(type(i)) # output = <class 'list'>
my goal is something like this:
looping a dictionary and making a list of each every keys
users={"glenn":1,"elena":2,"mama":3}
names=(list(users.keys()))
for i in names: # for each ? i dont know what is the correct approach of this
    # i should become a list of each 3 keys in a dictionay
    i=list(i)
    print(type(i))
print(type(glenn)) # confirming if its a list but, i think its impossible coz its outside of the for loop
I only want to loop the dictionary to get each keys as list item
You just want a list of keys? You already have that, in names (and you can drop the parens on line 2; they're unnecessary). Why would you think i should be a list?
im using a dictionary instead of manually writing the list out of names , coz each values and keys has purpose on another script and i want that if I add values and keys in that dictionary, list item will be added automatically
I'm not sure what you mean by "list item will be added automatically". Calling the keys method on the dict will always give you whatever the current keys are, so you can use that in a for loop to get the values. Perhaps you're overthinking?

Maybe you need to show the output you want and how you intend to use it. It still isn't clear why you want i to be a list in your examples. names is the list, so iterating over it in a for means i is assigned each of the values in the list (i.e. "glenn", "elena", "mama").
i dont know what is the correct approach , for each loop or for i, i dont have any idea,
all i need is to loop the dictionary to get each keys as list item

users={"glenn":1,"elena":2,"mama":3}
names=(list(users.keys()))
for i in names: # for each ? i dont know what is the correct approach of this
    # i should become a list of each 3 keys in a dictionay
    i=list(i)
    print(type(i))
print(type(glenn)) # confirming if its a list but, i think its impossible coz its outside of the for loop
i think i cannot make print a list that is outside of the for loop in this situation

users={"glenn":1,"elena":2,"mama":3}
names=(list(users.keys()))
namelist=[]
for i in names:
    print(i)
    namelist.append(i)
print(namelist)
i think this is all ive got,, thanks for your time,
I think you're confusing yourself. As I said, names is already a list containing the keys in users. You don't need to make yet another list containing the same values. Of course you can loop over names and print each item or do whatever with it.
Also, line 7 in your first example: there's no variable called glenn so you should get a NameError of course. If you wrote "glenn", that wouldn't be a list either since that value is a string.
Do you want a list of the dictionary keys, or do you want a list that contains the dictionary as a list? Should the end result look like this:

["glenn","elena","mama"]

or this:

[["glenn",1], ["elena", 2], ["mama",3]]

or something else?
(Jun-25-2020, 12:55 PM)deanhystad Wrote: [ -> ]Do you want a list of the dictionary keys, or do you want a list that contains the dictionary as a list? Should the end result look like this:

["glenn","elena","mama"]

or this:

[["glenn",1], ["elena", 2], ["mama",3]]

or something else?

i want something like this:
glenn=[ ]
elena=[ ]
mama=[ ]
is this possible using for loop in a dictionary or i have to use def ?
coz i dont want to declare those list, i want those list to be created on a for loop or in a def function if possible
The code you said did what you wanted in post 5 doesn't do that, sigh. Why don't you just have a dictionary mapping those keys to lists then? You really shouldn't try and dynamically create variables, because that will give you code that's overly complicated.