Python Forum
making list in looping a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making list in looping a dictionary
#1
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
Reply
#2
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?
Reply
#3
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
Reply
#4
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").
Reply
#5
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,
Reply
#6
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.
Reply
#7
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.
Reply
#8
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?
Reply
#9
(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
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class-Aggregation and creating a list/dictionary IoannisDem 1 282 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Looping through nested elements and updating the original list Alex_James 3 476 Aug-19-2021, 12:05 PM
Last Post: Alex_James
Question Making a copy list in a function RuyCab 1 513 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  looping a dictionary harmans14 1 753 Apr-11-2021, 08:48 PM
Last Post: bowlofred
  Python dictionary with values as list to CSV Sritej26 4 1,049 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  convert List with dictionaries to a single dictionary iamaghost 3 1,074 Jan-22-2021, 03:56 PM
Last Post: iamaghost
Sad Convert python list to dictionary akanowhere 6 1,343 Dec-27-2020, 09:26 PM
Last Post: Pedroski55
  Creating a dictionary from a list Inkanus 5 1,392 Nov-06-2020, 06:11 PM
Last Post: DeaD_EyE
  Dictionary list coja56 7 1,228 Sep-20-2020, 03:53 PM
Last Post: deanhystad
  How to make a list of values from a dictionary list? faryad13 2 864 Sep-03-2020, 03:45 PM
Last Post: faryad13

Forum Jump:

User Panel Messages

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