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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:48:40+00:00 2026-06-14T10:48:40+00:00

Very beginner question but it is driving me mad. sample1, sample2 etc. are Pygame.mixer.sound

  • 0

Very beginner question but it is driving me mad. sample1, sample2 etc. are Pygame.mixer.sound objects.

sample_list = []
sample_list.append(sample1)
sample_list.append(sample2)
sample_list.append(sample3)

Is fine, but I want to do that using a for style loop, e.g.

for j in range(1, 3, 1):
    sample_list.append(sample + j)

But that is trying to add a number to a sound object so isn’t right. I can add the equivalent string by;

for j in range(1, 3, 1):
    sample_list.append("sample" + str(j))

But that doesn’t refer to the objects I want, just adds those strings.
I’ve tried must permutations of syntax I can think of but it is still alluding me!
Thanks.

  • 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-14T10:48:41+00:00Added an answer on June 14, 2026 at 10:48 am

    I would recommend storing these in a dict to begin with. It is almost the same effect for you to reference by a name, but without the explicit object symbol for each:

    samples = {
        "sample1": Sample(),
        "sample2": Sample()
    }
    samples['sample3'] = Sample()
    

    This is the preferred approach when you have a dynamic number of objects you are creating and want to be able to grab them by a name later. You can store 100’s of these in your dict without cluttering up your namespace.

    And later if you are trying to apply this to your loop, you can reference the string names:

    for i in xrange(1,4):
        sample_list.append(samples["sample" + str(i)])
    

    As a side note another way to get attributes by name when they live on some object is to use getattr. Assume you have this:

    class Sampler(object):
        pass
    
    sampler = Sampler()
    sampler.sample1 = Sample()
    sampler.sample2 = Sample()
    sampler.sample3 = Sample()
    

    You can then reference by name via: getattr(sampler, "sample1")

    Note: As mentioned in comments by @Marcin, if you don’t care about having a string identifier to be able to look up your items, and they are just purely sequential by number, you can use this same approach but with a list. It depends on your needs.

    It is possible you might want to end up doing something like:

    samples = {
        "bang1": Sample(),
        "bang2": Sample(),
        "bang3": Sample(),
        "shot1": Sample(),
        "shot2": Sample(),
        ...
    }
    

    … Which would then allow you to look up sequential subsets of those sounds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very beginner question, but driving me crazy. Why isn't this code
i know this question was asked before but i have this very weird beginner
This is a very beginner question. I have tried to search for advice but
this is a very beginner question but I don't know javascript, I only wants
I know this is a very beginner question, but I've been struggling to figure
Okay, this might be a very silly beginner question, but: I've got an ClassA,
I am very beginner in Ruby and probably the question is too easy but
Sorry that this is very much a beginner's question, but yes, I'm only just
Once again a very beginner-ish question, but here I go: I would like to
Ok so this is probably a very beginner question but... Right now I have

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.