What is the recommended way to randomize a list of strings using Python

0 votes
Hi all, with regard to the above topic - my question is simple. Let me elaborate - So my input in this particular case is a list of strings. And the output I expect is a list with the same strings but in random order.

In case if there are any duplicates then it must be considered too - As in, if the same string can appear more than once in the input, the same follows through in the output as well.

I am aware of methods to go about achieving this like using loops for example. And this is what I am actually doing for now.

I am also aware that Python is amazingly easy to work with and there is a one-line statement which is used to cover this job. So, help?
Jan 18, 2019 in Python by Anirudh
• 2,090 points
968 views

1 answer to this question.

0 votes

Hi. Nice question.

Here is the simplified answer for you:

>>> import random
>>> x = [1, 2, 3, 4, 3, 4]
>>> random.shuffle(x)
>>> x
[4, 4, 3, 1, 2, 3]
>>> random.shuffle(x)
>>> x
[3, 4, 2, 1, 3, 4]

Random.shuffle does this exact job.

However, if you are open to more reading then I suggest reading the Fisher-Yates Shuffle methodology. 

Hope this helped!

answered Jan 18, 2019 by Nymeria
• 3,560 points

Related Questions In Python

0 votes
1 answer

What is the function to randomize the items of a list in-place?

Hello @Roshni, Python has a built-in module called ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,770 points
1,388 views
0 votes
1 answer

What is the preferred way to concatenate strings in python??

If the strings you are concatenating are ...READ MORE

answered Dec 21, 2018 in Python by charlie_brown
• 7,720 points
849 views
0 votes
1 answer

Is there a way to list out in-built variables and functions of Python?

The in-built variables and functions are defined ...READ MORE

answered May 14, 2019 in Python by Junaid
2,245 views
0 votes
1 answer

What is the best way to remove accents in a Python unicode string?

Hello @kartik, Some languages have combining diacritics as ...READ MORE

answered May 11, 2020 in Python by Niroj
• 82,840 points
7,313 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,466 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,127 views
0 votes
1 answer
0 votes
1 answer

What is the equivalent of NotImplementedError using Python?

Hi, good question! One simple answer to your ...READ MORE

answered Jan 17, 2019 in Python by Nymeria
• 3,560 points
2,349 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP