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 6765105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:40:23+00:00 2026-05-26T14:40:23+00:00

So I have an assignment that is due later tonight and I’m stuck on

  • 0

So I have an assignment that is due later tonight and I’m stuck on this one definition. What I have is a list of random terms:

['i', 'am', 'a', 'brown', 'cow']

And what I want to do with this list is associate each term to a value/term:

[['i', ['term']], ['am', ['term']], ['a', ['term']], ['brown', ['term']], ['cow', ['term']]]

My thoughts on how to get started on this was to use a for loop and append the term after each key term and somehow try to separate each pair.

  • 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-05-26T14:40:24+00:00Added an answer on May 26, 2026 at 2:40 pm

    If you are a beginner in Python, the most obvious option probably would be simple loop approach:

    d = {}
    lst = ['i', 'am', 'a', 'brown', 'cow']
    for key in lst:
      d[key] = 'term'
    

    After this you can access your elements like this:

    In:  print d['am']
    Out: 'term'
    

    Of course, this is not the best way to do this. As other have already said, you should take a look at list comprehension and builtin zip() function. I will try to write some examples of code using list comprehension and zip() function, but it is still highly recommended to read the official documentation.

    You should note that list comprehension always gives you a list as a result, so the best thing you can get is what you have demonstrated in your question. Here is a small example:

    lst = ['i', 'am', 'a', 'brown', 'cow']
    res = [[item, ['term']] for item in lst]
    

    This will give you exactly the same you have written in your question. In case you want to get a dict (as in our first example), you might want to modify this code slightly:

    lst = ['i', 'am', 'a', 'brown', 'cow']
    res = [(item, 'term') for item in lst]
    d = dict(res)
    

    And again you have:

    In : print d['am']
    Out: 'term'
    

    Now, you probably want to assign different terms to different elements of your list.
    Let’s say you have two lists of the same length, one with elements, the other with terms:

    lst = ['i', 'am', 'a', 'brown', 'cow']
    terms = ['term1', 'term2', 'term3', 'term4', 'term5']
    

    This is where zip() function helps. If you pass it lst and terms as arguments, it will give you a list of tuples (just as in our res variable a few lines before):

    In : zip(lst, terms)
    Out: [('i', 'term1'), ('am', 'term2'), ('a', 'term3'), ('brown', 'term4'), ('cow', 'term5')]
    

    And now you can again use dict to create a new dictionary:

    what_i_need = dict(zip(lst, terms))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this assignment due that requires the usage of FLTK. The code is
For my Operating Systems class I have an assignment due that is built onto
I have Assignment due in which i'm stuck on a question. Add a Sales
Sorry if my title is confusing. I have an assignment due that has to
I have an assignment that wants me to write an ternary search algorithm and
I have an assignment that simulates a dice game. As part of the program,
I have quite a large XSL document for an assignment that does a number
My assignment requires that I have an ER diagram that shows the dependencies between
For a school assignment I have to create a C++ program that will create
For an assignment in college, we have to make a script in Perl that

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.