Python Forum
making list in looping a dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: making list in looping a dictionary (/thread-27876.html)



making list in looping a dictionary - glennford49 - Jun-25-2020

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


RE: making list in looping a dictionary - ndc85430 - Jun-25-2020

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?


RE: making list in looping a dictionary - glennford49 - Jun-25-2020

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


RE: making list in looping a dictionary - ndc85430 - Jun-25-2020

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").


RE: making list in looping a dictionary - glennford49 - Jun-25-2020

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,


RE: making list in looping a dictionary - ndc85430 - Jun-25-2020

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.


RE: making list in looping a dictionary - ndc85430 - Jun-25-2020

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.


RE: making list in looping a dictionary - deanhystad - Jun-25-2020

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?


RE: making list in looping a dictionary - glennford49 - Jun-25-2020

(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


RE: making list in looping a dictionary - ndc85430 - Jun-25-2020

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.