Shuffle list python
In this article, you will learn how to shuffle a list using the Python programming language.
A list is a sequence of indexed and ordered values, like an array. It is mutable, which means we can change the order of elements in a list. It contains a list of any type of data object with a comma separated and enclosed within a square bracket. Each element of the list has an index, starting with 0, 1, and so on. In the development process, we may come to a situation in which we need to shuffle a list.
Shuffling a list of numbers has consistently been a valuable utility and the question that has shown up in many organization interviews too. Knowing beyond what one strategy to accomplish this can generally be an or more. We should examine certain manners by which this can be accomplished.
The random module of Python provides an inbuilt method shuffle() to randomize the items of lists, strings, and tuples. This method shuffles the original items of lists, strings, and tuples. This is the most recommended method to shuffle a list.
Syntax of shuffle()
Here, the list is any sequence or tuple that you want to shuffle, and random is the optional parameter. It is a function returning a random floating number between 0.1 and 1.0. If not specified, by default it takes a random.random() function.
Python shuffle a range value
In the given example, we have used a random module to perform the random generations on a list and have taken a range of 7 numbers and used the random.shuffle() method to shuffle the given list in place.
Output of the above code:
import random x = list(range(7)) print(x) random.shuffle(x) print(x)
[0, 1, 2, 3, 4, 5, 6] [0, 1, 6, 2, 3, 5, 4]
[0, 1, 2, 3, 4, 5, 6] [2, 6, 0, 3, 1, 5, 4]
Python shuffle a list
In the given Python program, we have initialized the list items and shuffled them using the random.shuffle() method.
Output of the above code:
import random list1 = [34, 35, 73, 22, 13, 6, 23, 7, 31, 70] print("Original list:", list1) random.shuffle(list1) print("First shuffled list:", list1) random.shuffle(list1) print("Second shuffled list:", list1)
Original list: [34, 35, 73, 22, 13, 6, 23, 7, 31, 70] First shuffled list: [13, 70, 73, 7, 22, 31, 35, 23, 34, 6] Second shuffled list: [22, 23, 7, 6, 13, 73, 31, 34, 70, 35]
In the above examples, you have seen that the random.shuffle() method shuffles the original lists. But what if we want to shuffle not in-place, then we need to use a random.sample() function. The random.sample() method returns a new shuffled list. The original list remains unchanged.
Syntax of random.sample()
Here, the sequence can be any sequence, list, tuple, string that you want to shuffle, and n is the integer value that specifies the length of a sample. It returns the random list with the sample size you passed to it. For example, sample(number_list, 5) will return a list of 5 random items from a list.
Python example of random.sample()
In the given example, we have shuffled a list using the random.sample() method.
from random import sample number_list = [43, 12, 19, 6, 23, 17, 31] print("First shuffled list:", sample(number_list,5)) print("First shuffled list:", sample(number_list,3)) print("Original list:", number_list)
We got the following output-
First shuffled list: [12, 17, 6, 31, 23] First shuffled list: [23, 17, 43] Original list: [43, 12, 19, 6, 23, 17, 31]
Python multiline string with variables
Python glob recursive
Python max heap implementation
Zip in Python for loop
Remove element from list Python
Check if list is empty Python
Python remove element from list by value
VADER sentiment analysis on pandas dataframe
Python isalpha character
Script to download YouTube videos
Python projects for practice
Python string to datetime
Fillna pandas with mean
Lemmatization Python dataframe
How to generate QR Code in Python using PyQRCode
OpenCV and OCR Python
PHP code to send SMS to mobile from website
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml