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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:43:59+00:00 2026-05-26T20:43:59+00:00

in this program I’m trying to shuffle a list by randomly choosing two items

  • 0

in this program I’m trying to shuffle a list by randomly choosing two items from a list and swapping them round, and then repeating this process several times.

The problem I have encountered is I don’t know how I can swap the items round and print the shuffled list.

For instance if my two random values were a and b, if I were to just put:

a = b
b = a

then that would change the value of a to b, but when it tries to change b to a, no change would occur as a has already been changed to b.

The only way I can think that this would work is swapping them at the same time, but I do not know of a function/ way to swap them round.

Also if a, b were items of a list L, after I swapped them round if I used

print L

should it print the altered version? I only ask because from what I have tried it is not doing that.

NB I am trying to shuffle this list stage by stage by swapping, instead of using the shuffle function imported from random.

  • 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-26T20:44:00+00:00Added an answer on May 26, 2026 at 8:44 pm

    Use a temp variable for your first problem:

    temp = a
    a = b
    b = temp
    

    In Python you can also do this:

    a, b = b, a
    

    I suspect your second problem is because you’re changing things you got out of the list, instead of changing the list. Try this:

    i, j = # two indexes to swap in the list
    L[i], L[j] = L[j], L[i]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program has the user input name / age pairs and then outputs them,
This program reads strings of numbers from a txt file, converts them to integers,
This program takes 2 numbers from user input, asks them whether they'd like to
This program adds two matrices but it's giving too many errors and I can't
This program so far has one purpose, take in two integers(the size of the
This program is written in C++. I am trying to use a void function
this program accepts user input and saved to a char array. Then creates a
This program is really getting on my nerves: I am trying to read a
This program is from effective java 2nd edition book, I couldn't understand how the
//This program evaluates an expression of two fractions added or subtracted #include <iostream> #include

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.