0

I'm trying to create a directory of folders and files for each user, and I can't seem to access a dictionary for a folder within a list of all of a user's files but only using the folder name, not by indexing.

I could iterate over the list and check if the element is a dictionary with the folder name I'm trying to access, but I felt like there was probably an easier way.

userDir = {"User1": [{"folder1": [file1, file2]}, file3], "User2": []}

What's the best way I could access the dictionary key "folder1" here?

10
  • You just need to drill down through the dictionary, then enumerate lists: userDir['User1'][1] this will give file3, userDir['User1'][0]['folder1'] gets you to the list of file1 & file2 and this, userDir['User1'][0]['folder1'][0], gets you to file one in the dictionary, list, dictionary, list structure: Commented Jan 20, 2019 at 21:21
  • Sorry, but I want a way to access the "folder1" dictionary if you don't know which position it is inside the list of "User1" Commented Jan 20, 2019 at 21:26
  • 1
    You cannot, you need to access the whole list, then iterate through it, lists are by nature ordered, fixed and enumerated. If you do not know the numeric position then you have not choice but to pull the list and iterate through the items on the list to view the keys/ items. It will take a well thought our function.... Commented Jan 20, 2019 at 21:30
  • Try something, post it here and then ask for feedback once you have an attempt. Commented Jan 20, 2019 at 21:31
  • How is this marked as duplicate?? Commented Jan 20, 2019 at 21:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.