Python List Shuffle
You can shuffle Python list by importing random
and using shuffle
.
import random
a = [1, 2, 3]
random.shuffle(a)
print(a) # [2, 1, 3]
shuffle()
updates the original list and returns None.
import random
a = [1, 2, 3]
b = random.shuffle(a)
print(b) # None
Random seed
You can set the seed by random.seed() globally.
import random
random.seed(7)
a = [1, 2, 3]
random.shuffle(a)
print(a) # [3, 1, 2]
The sorted list is always same whenever running the program. If you don't set the seed, the result changes every running.
Python List
- Get the number of elements
- Append an element
- Extend a list
- Reverse a list
- Sort a list
- Shuffle a list
- Get the index of an element
- Pop an element
- Delete an element from a list
- Clear a list
- Empty list
- Python range
- Get the range of integers
- Get a random element from a list
- Join a list
- Get keys and values iterating a list
- List comprehension
- Get the last element
- Insert an object at the first position
- Get the index of the max value
- Convert a list to a tuple
- Convert a list to json
- Filter
- Iterate lists in parallel
- Generator
- Make a cartesian product
- Sort a list of tuples by the first or second element