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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:57:37+00:00 2026-05-26T15:57:37+00:00

I am trying to populate a list of 8 Ingredient objects using one list

  • 0

I am trying to populate a list of 8 Ingredient objects using one list comprehension expression.
The code looks like that:

import random
ings = (('w1', 200, 25, 80),
   ('su1', 50, 55, 150),
   ('su2', 400, 100, 203),
   ('sy1', 10, 150, 355),
   ('sy2', 123, 88, 101),
   ('sy3', 225, 5, 30),
   ('sy4', 1, 44, 99),
   ('sy5', 500, 220, 300),)

class Ingredient: 
    def __init__(self, n, p, mi, ma):
        self.name = n
        self.price = p
        self.min = mi
        self.max = ma
        self.perc = random.randrange(mi, ma)

class Drink:
    def __init__(self): 
        self.ing = []

and i would like to obtain result equivalent to this:

self.ing = [Ingredient('w1', 200, 25, 80), Ingredient('su1', 50, 55, 150) ... 
(and so it goes for the ings tuple) ]

Now, my question is how to do it using LCE or if there is more optimal way of doing this (in terms of code readability or speed)?

  • 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-26T15:57:38+00:00Added an answer on May 26, 2026 at 3:57 pm
    [Ingredient(*ing) for ing in ings]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to populate a dictionary dynamically in a for loop like so: pseudo-code
I have a list box that I'm trying to populate with a list of
I'm trying to populate a listviewactivity's list with objects returned from an DB40 database,
i'm trying to populate a <mx:List> with a dataProvider that i received from a
I'm trying to populate a db.ListProperty() model field using bulkloader. I'm using an import
I am trying to populate a list of images dynamically using javascript. I have
I'm trying to populate a Drop down list with pharmaceutical companies, like Bayer, Medley
Using PHP, I'm trying to populate an HTML list with data from two different
I'm trying to populate a list of files, and I'm using a custom ArrayAdapter
I am trying to populate an HTML select list using an array of customer

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.