shuffle() in python by R4R Team

- Shuffle() is a python function which is predefined in random library
- It is used in shuffle the sequence like list.
- Shiffle don't work on the set and tuple because they are immutable data type in python.

Example-

list is [2,3,4,5,8]

after shuffle it may be [3,8,4,2,5] or [8,2,5,4,3] etc.

program-


import random
print("Enter element in list")
l=list(map(int,input().split()))
print("Original list is ",l)
random.shuffle(l)
print("Shuffle list is ",l)


output -

Enter element in list
2 3 5 4 7 8
Original list is [2, 3, 5, 4, 7, 8]
Shuffle list is [7, 3, 4, 5, 8, 2]

Enter element in list
12 3 45 67 21
Original list is [12, 3, 45, 67, 21]
Shuffle list is [21, 12, 67, 3, 45]


-In this program,we import the random library by import random, because here we use the shuffle() function which is defined in random library, if we do not import random library then shuffle() gives an error like undefined symbol shuffle()
- We take input of numeric string and with the help of split() and map() we convert into list, then using random.shuffle() we shuffle the elements in list and print the list to show the output.


Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!