Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9059331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:56:54+00:00 2026-06-16T14:56:54+00:00

I have basically a list of all the files in a folder, which in

  • 0

I have basically a list of all the files in a folder, which in a simplified version looks like :

file_list = [ 'drug.resp1.17A.tag', 'drug.resp1.96A.tag', 'drug.resp1.56B.tag', 'drug.resp2.17A.tag', 'drug.resp2.56B.tag', 'drug.resp2.96A.tag']

Another list :

drug_list = [ '17A', '96A', '56B']

I want to combine these two list into a dictionary, such that:

dictionary = {
    '17A' : ['drug.resp1.17A.tag' , 'drug.resp2.17A.tag' ], 
    '96A' : ['drug.resp1.96A.tag' , 'drug.resp2.96A.tag' ], 
    '56B' : ['drug.resp1.56B.tag' , 'drug.resp2.56B.tag' ]}

I thought of doing like this but got stuck !

dict_drugs = {}
for file in file_list:
    list_filename = file.split('.')
    for elem in drug_list:
        if elem in list_filename:

What can I do after this to join the elements into a dictionary, or am I doing this completely wrong ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:56:56+00:00Added an answer on June 16, 2026 at 2:56 pm

    well you don’t need inner loop

    >>> file_list = [ 'drug.resp1.17A.tag', 'drug.resp1.96A.tag', 'drug.resp1.56B.tag', 'drug.resp2.17A.tag', 'drug.resp2.56B.tag', 'drug.resp2.96A.tag']
    >>> dictonary = {}
    ... for i in file_list:
    ...     k = i.split('.')[-2]
    ...     if k in dictonary:
    ...         dictonary[k].append(i)
    ...     else:
    ...         dictonary[k] = [i]
    >>> dictonary
    62: {'17A': ['drug.resp1.17A.tag', 'drug.resp2.17A.tag'],
     '56B': ['drug.resp1.56B.tag', 'drug.resp2.56B.tag'],
     '96A': ['drug.resp1.96A.tag', 'drug.resp2.96A.tag']}
    >>> 
    

    one more check if only needs those values that are present in drug_list

    means if file_list contains :

    file_list = [ ‘drug.resp1.18A.tag‘, ‘drug.resp1.96A.tag’, ‘drug.resp1.56B.tag’, ‘drug.resp2.17A.tag’, ‘drug.resp2.56B.tag’, ‘drug.resp2.96A.tag’]

    >>> drug_list = [ '17A', '96A', '56B']
    ... dictonary = {}
    ... for i in file_list:
    ...     k = i.split('.')[-2]
    ...     if k in drug_list:
    ...         if k in dictonary:
    ...             dictonary[k].append(i)
    ...         else:
    ...             dictonary[k] = [i]
    >>> 
    

    One more way to efficently do upper case:

    dictonary = dict(((i,[]) for i in drug_list))
    
    dictonary = {drug: [] for drug in drug_list}  # As @J.F. Sebastian suggested.
    
    for file in file_list:
        k = file.split('.')[-2]
        if k in dictonary:
            dictonary[k].append(file)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have this application which scans through all mp3's in a folder and
I have an array of folders/files that looks like this: Array ( [Root Folder
Basically I have a list of files composed from a foreach loop that all
I have a string which is basically a list of words delimited by commas.
Basically I have a draggable list which is connected with a sortable list. Works
I have a series of index files for some data files which basically take
I have some R code which performs some data extraction operation on all files
I have a list of folders which contain lots of text files. Inside those
So I have site list on certain pages that basically just have links to
I have this small piece of code that basically takes a list and runs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.