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
  Dictionary in a list bashage 2 493 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 597 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  for loops break when I call the list I'm looping through Radical 4 823 Sep-18-2023, 07:52 AM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  check if element is in a list in a dictionary value ambrozote 4 1,879 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,896 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 1,531 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,523 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,880 Oct-03-2021, 05:16 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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