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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:22:10+00:00 2026-05-13T10:22:10+00:00

I have a list[] of items from which I’d like to display one randomly,

  • 0

I have a list[] of items from which I’d like to display one randomly, but the displayed item must not repeat more than once in last x requests.

  1. list1 = item1, item2, item3, item4,
    item5, item6, item7, item8, item9,
    item 10
  2. Display a random selection
    from the list above
  3. list2 = store the last displayed item in list2 which should only store 7
    items, not more
  4. Display a random
    selection from the list but make
    sure it doesn’t exist in the
    list2

Is that the right way to do it? Either way, I’d like to know how to limit a list to store only 7 items?

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-05-13T10:22:11+00:00Added an answer on May 13, 2026 at 10:22 am

    collections.deque is the only sequence type in python that naturally supports being bounded (and only in Python 2.6 and up.) If using python 2.6 or newer:

    # Setup
    from collections import deque
    from random import choice
    used = deque(maxlen=7)
    
    # Now your sampling bit
    item = random.choice([x for x in list1 if x not in used])
    used.append(item)
    

    If using python 2.5 or less, you can’t use the maxlen argument, and will need to do one more operation to chop off the front of the deque:

    while len(used) > 7:
        used.popleft()
    

    This isn’t exactly the most efficient method, but it works. If you need speed, and your objects are hashable (most immutable types), consider using a dictionary instead as your “used” list.

    Also, if you only need to do this once, the random.shuffle method works too.

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

Sidebar

Related Questions

I have a method which is list my items from a server. Therefore, this
Say I have a list of items from a database. Next to each item
I have a list of items that are displayed using a ListView from a
I have a source list from which I am picking up random items and
I want to display list of items which belong to certain category like this:
I have a List of Items which I retrieved from my Sqlite DB... I
I have a list of items, each of which contains a display property of
I have a list of items that are created from the results of website
Using the method from Steve Sanderson's blog , I have a list of items
I have a dynamically created list. Items from the list should be able to

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.