Related
How do I retrieve an item at random from the following list?
foo = ['a', 'b', 'c', 'd', 'e']
Use random.choice():
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use secrets.choice():
import secrets
foo = ['battery', 'correct', 'horse', 'staple']
print(secrets.choice(foo))
secrets is new in Python 3.6. On older versions of Python you can use the random.SystemRandom class:
import random
secure_random = random.SystemRandom()
print(secure_random.choice(foo))
If you want to randomly select more than one item from a list, or select an item from a set, I'd recommend using random.sample instead.
import random
group_of_items = {'a', 'b', 'c', 'd', 'e'} # a sequence or set will work here.
num_to_select = 2 # set the number to select here.
list_of_random_items = random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1]
If you're only pulling a single item from a list though, choice is less clunky, as using sample would have the syntax random.sample(some_list, 1)[0] instead of random.choice(some_list).
Unfortunately though, choice only works for a single output from sequences (such as lists or tuples). Though random.choice(tuple(some_set)) may be an option for getting a single item from a set.
EDIT: Using Secrets
As many have pointed out, if you require more secure pseudorandom samples, you should use the secrets module:
import secrets # imports secure module.
secure_random = secrets.SystemRandom() # creates a secure random object.
group_of_items = {'a', 'b', 'c', 'd', 'e'} # a sequence or set will work here.
num_to_select = 2 # set the number to select here.
list_of_random_items = secure_random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1]
EDIT: Pythonic One-Liner
If you want a more pythonic one-liner for selecting multiple items, you can use unpacking.
import random
first_random_item, second_random_item = random.sample({'a', 'b', 'c', 'd', 'e'}, 2)
If you also need the index, use random.randrange
from random import randrange
random_index = randrange(len(foo))
print(foo[random_index])
As of Python 3.6 you can use the secrets module, which is preferable to the random module for cryptography or security uses.
To print a random element from a list:
import secrets
foo = ['a', 'b', 'c', 'd', 'e']
print(secrets.choice(foo))
To print a random index:
print(secrets.randbelow(len(foo)))
For details, see PEP 506.
I propose a script for removing randomly picked up items off a list until it is empty:
Maintain a set and remove randomly picked up element (with choice) until list is empty.
s=set(range(1,6))
import random
while len(s)>0:
s.remove(random.choice(list(s)))
print(s)
Three runs give three different answers:
>>>
set([1, 3, 4, 5])
set([3, 4, 5])
set([3, 4])
set([4])
set([])
>>>
set([1, 2, 3, 5])
set([2, 3, 5])
set([2, 3])
set([2])
set([])
>>>
set([1, 2, 3, 5])
set([1, 2, 3])
set([1, 2])
set([1])
set([])
foo = ['a', 'b', 'c', 'd', 'e']
number_of_samples = 1
In Python 2:
random_items = random.sample(population=foo, k=number_of_samples)
In Python 3:
random_items = random.choices(population=foo, k=number_of_samples)
NumPy solution: numpy.random.choice
For this question, it works the same as the accepted answer (import random; random.choice()), but I added it because the programmer may have imported NumPy already (like me)
And also there are some differences between the two methods that may concern your actual use case.
import numpy as np
np.random.choice(foo) # randomly selects a single item
For reproducibility, you can do:
np.random.seed(123)
np.random.choice(foo) # first call will always return 'c'
For samples of one or more items, returned as an array, pass the size argument:
np.random.choice(foo, 5) # sample with replacement (default)
np.random.choice(foo, 5, False) # sample without replacement
If you need the index, just use:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print int(random.random() * len(foo))
print foo[int(random.random() * len(foo))]
random.choice does the same:)
How to randomly select an item from a list?
Assume I have the following list:
foo = ['a', 'b', 'c', 'd', 'e']
What is the simplest way to retrieve an item at random from this list?
If you want close to truly random, then I suggest secrets.choice from the standard library (New in Python 3.6.):
>>> from secrets import choice # Python 3 only
>>> choice(list('abcde'))
'c'
The above is equivalent to my former recommendation, using a SystemRandom object from the random module with the choice method - available earlier in Python 2:
>>> import random # Python 2 compatible
>>> sr = random.SystemRandom()
>>> foo = list('abcde')
>>> foo
['a', 'b', 'c', 'd', 'e']
And now:
>>> sr.choice(foo)
'd'
>>> sr.choice(foo)
'e'
>>> sr.choice(foo)
'a'
>>> sr.choice(foo)
'b'
>>> sr.choice(foo)
'a'
>>> sr.choice(foo)
'c'
>>> sr.choice(foo)
'c'
If you want a deterministic pseudorandom selection, use the choice function (which is actually a bound method on a Random object):
>>> random.choice
<bound method Random.choice of <random.Random object at 0x800c1034>>
It seems random, but it's actually not, which we can see if we reseed it repeatedly:
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
A comment:
This is not about whether random.choice is truly random or not. If you fix the seed, you will get the reproducible results -- and that's what seed is designed for. You can pass a seed to SystemRandom, too. sr = random.SystemRandom(42)
Well, yes you can pass it a "seed" argument, but you'll see that the SystemRandom object simply ignores it:
def seed(self, *args, **kwds):
"Stub method. Not used for a system random number generator."
return None
I usually use the random module for working with lists and randomization:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
In short, use random.sample method
The sample method returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples.
import random
lst = ['a', 'b', 'c', 'd', 'e']
random.seed(0) # remove this line, if you want different results for each run
rand_lst = random.sample(lst,3) # 3 is the number of sample you want to retrieve
print(rand_lst)
Output:['d', 'e', 'a']
here is a running code
https://onecompiler.com/python/3xem5jjvz
This is the code with a variable that defines the random index:
import random
foo = ['a', 'b', 'c', 'd', 'e']
randomindex = random.randint(0,len(foo)-1)
print (foo[randomindex])
## print (randomindex)
This is the code without the variable:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print (foo[random.randint(0,len(foo)-1)])
And this is the code in the shortest and smartest way to do it:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
(python 2.7)
Random item selection:
import random
my_list = [1, 2, 3, 4, 5]
num_selections = 2
new_list = random.sample(my_list, num_selections)
To preserve the order of the list, you could do:
randIndex = random.sample(range(len(my_list)), n_selections)
randIndex.sort()
new_list = [my_list[i] for i in randIndex]
Duplicate of https://stackoverflow.com/a/49682832/4383027
You could just:
from random import randint
foo = ["a", "b", "c", "d", "e"]
print(foo[randint(0,4)])
This may already be an answer, but you can use random.shuffle. Example:
import random
foo = ['a', 'b', 'c', 'd', 'e']
random.shuffle(foo)
The recommended numpy way is now to use an explicit RNG:
from numpy.random import default_rng
rng = default_rng()
rng.choice(foo)
We can also do this using randint.
from random import randint
l= ['a','b','c']
def get_rand_element(l):
if l:
return l[randint(0,len(l)-1)]
else:
return None
get_rand_element(l)
How would I unappend these appended variable:
if letterchoice == wordchoice[0]:
wc = []
for each in wordchoice:
wc.append(each)
wh = []
for each in wordhidden:
wh.append(each)
wh[0] = wc[0]
This is for part of a hangman program(I understand that this is probably not the most efficient way of doing this but I am new to python and wanted to give myself a challenge)
There are several ways you can remove item from a list, you can delete by the value or by the index:
>>> foo = [1,2,3,4]
>>> foo.pop(1)
2
>>> foo
[1, 3, 4]
>>> foo.remove(1)
>>> foo
[3, 4]
>>> del foo[1]
>>> foo
[3]
>>>
Nevertheless, I suggest to use .pop().
Edit: Based on your comment, is this what you want to do?
>>> foo = ['h', 'm', 'f', 'd', 's']
>>> foo
['h', 'm', 'f', 'd', 's']
>>> ''.join(foo)
'hmfds'
>>>
Use the python list pop() method, which removes and returns last object or obj from the list i.e.
wh.pop()
To avoid popping from an empty list, consider checking first:
if wh: # Makes sure there is something to pop()
wh.pop()
Also, you may want to "catch" the popped entry and avoid it from being printed to the terminal, as in:
if wh:
x = wh.pop()
I'm new to programming in general, so looking to really expand my skills here. I'm trying to write a script that will grab a list of strings from an object, then order them based on a template of my design. Any items not in the template will be added to the end.
Here's how I'm doing it now, but could someone suggest a better/more efficient way?
originalList = ['b', 'a', 'c', 'z', 'd']
listTemplate = ['a', 'b', 'c', 'd']
listFinal = []
for thing in listTemplate:
if thing in originalList:
listFinal.append(thing)
originalList.pop(originalList.index(thing))
for thing in originalList:
listFinal.append(thing)
originalList.pop(originalList.index(thing))
Try this:
originalList = ['b', 'a', 'c', 'z', 'd']
listTemplate = ['a', 'b', 'c', 'd']
order = { element:index for index, element in enumerate(listTemplate) }
sorted(originalList, key=lambda element: order.get(element, float('+inf')))
=> ['a', 'b', 'c', 'd', 'z']
This is how it works:
First, we build a dictionary indicating, for each element in listTemplate, its relative order with respect to the others. For example a is 0, b is 1 and so on
Then we sort originalList. If one of its elements is present in the order dictionary, then use its relative position for ordering. If it's not present, return a positive infinite value - this will guarantee that the elements not in listTemplate will end up at the end, with no further ordering among them.
The solution in the question, although correct, is not very pythonic. In particular, whenever you have to build a new list, try to use a list comprehension instead of explicit looping/appending. And it's not a good practice to "destroy" the input list (using pop() in this case).
You can create a dict using the listTemplate list, that way the expensive(O(N)) list.index operations can be reduced to O(1) lookups.
>>> lis1 = ['b', 'a', 'c', 'z', 'd']
>>> lis2 = ['a', 'b', 'c', 'd']
Use enumerate to create a dict with the items as keys(Considering that the items are hashable) and index as values.
>>> dic = { x:i for i,x in enumerate(lis2) }
Now dic looks like:
{'a': 0, 'c': 2, 'b': 1, 'd': 3}
Now for each item in lis1 we need to check it's index in dic, if the key is not found we return float('inf').
Function used as key:
def get_index(key):
return dic.get(key, float('inf'))
Now sort the list:
>>> lis1.sort(key=get_index)
>>> lis1
['a', 'b', 'c', 'd', 'z']
For the final step, you can just use:
listFinal += originalList
and it will add these items to the end.
There is no need to create a new dictionary at all:
>>> len_lis1=len(lis1)
>>> lis1.sort(key = lambda x: lis2.index(x) if x in lis2 else len_lis1)
>>> lis1
['a', 'b', 'c', 'd', 'z']
Here is a way that has better computational complexity:
# add all elements of originalList not found in listTemplate to the back of listTemplate
s = set(listTemplate)
listTemplate.extend(el for el in originalList if el not in s)
# now sort
rank = {el:index for index,el in enumerate(listTemplate)}
listFinal = sorted(originalList, key=rank.get)
I want to create a dictionary as following -
{'a':[1, 2, 3, 4, 5], 'b':[1, 3, 5], 'c':[2, 3, 5]}
The way I have implemented it is
mydict = dict()
letters = ['a', 'b', 'a', 'c', 'a']
#please mark the list has multiple occurence of a,
#hence I would want to check if a key with 'a' exists. Please do not advise to make the list unique.
for l in letters:
if not mydict.get(l):
mydict[l] = <values from another place via some filter>
else:
mydict[l].append(<values from another dict>)
Is there a better approach to do this?
Yes, you can use the defaultdict:
Sample code:
»»» from collections import defaultdict
»»» mydict = defaultdict(list)
»»» letters = ['a', 'b', 'a', 'c', 'a']
»»» for l in letters:
....: mydict[l].append('1')
....:
»»» mydict
Out[15]: defaultdict(<type 'list'>, {'a': ['1', '1', '1'], 'c': ['1'], 'b': ['1']})
If you need the content to be initialised to something fancier, you can specify your own construction function as the first argument to defaultdict. Passing context-specific arguments to that constructor might be tricky though.
The solution provided by m01 is cool and all but I believe it's worth mentionning that we can do that with a plain dict object..
mydict = dict()
letters = ['a', 'b', 'a', 'c', 'a']
for l in letters:
mydict.setdefault(l, []).append('1')
the result should be the same. You'll have a default dict instead of using a subclass. It really depends on what you're looking for. My guess is that the big problem with my solution is that it will create a new list even if it is not needed.
The defaultdict object has the advantage to create a new object only when something is missing. This solution has the advantage to be a simple dict without nothing special.
Edit
After thinking about it, I found out that using setdefault on a defaultdict will work as expected. But it's not yet good enough to say that a plain old dict should be used instead. There are cases where having a dict is important. To make it short, an invalid key on a dict will raise a KeyError. A defaultdict will return a default value.
As an example, there is the traversal algorithm that stops whenever it catches a KeyError or it traversed a whole path. With a defaultdict, you'd have to raise yourself the KeyError in case of errors.
You could use a defaultdict. (See reference)
from collections import defaultdict
mydict = defaultdict(list)
letters = ['a', 'b', 'a', 'c', 'a']
for l in letters:
mydict[l].append(<some data>)
How do I retrieve an item at random from the following list?
foo = ['a', 'b', 'c', 'd', 'e']
Use random.choice():
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use secrets.choice():
import secrets
foo = ['battery', 'correct', 'horse', 'staple']
print(secrets.choice(foo))
secrets is new in Python 3.6. On older versions of Python you can use the random.SystemRandom class:
import random
secure_random = random.SystemRandom()
print(secure_random.choice(foo))
If you want to randomly select more than one item from a list, or select an item from a set, I'd recommend using random.sample instead.
import random
group_of_items = {'a', 'b', 'c', 'd', 'e'} # a sequence or set will work here.
num_to_select = 2 # set the number to select here.
list_of_random_items = random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1]
If you're only pulling a single item from a list though, choice is less clunky, as using sample would have the syntax random.sample(some_list, 1)[0] instead of random.choice(some_list).
Unfortunately though, choice only works for a single output from sequences (such as lists or tuples). Though random.choice(tuple(some_set)) may be an option for getting a single item from a set.
EDIT: Using Secrets
As many have pointed out, if you require more secure pseudorandom samples, you should use the secrets module:
import secrets # imports secure module.
secure_random = secrets.SystemRandom() # creates a secure random object.
group_of_items = {'a', 'b', 'c', 'd', 'e'} # a sequence or set will work here.
num_to_select = 2 # set the number to select here.
list_of_random_items = secure_random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1]
EDIT: Pythonic One-Liner
If you want a more pythonic one-liner for selecting multiple items, you can use unpacking.
import random
first_random_item, second_random_item = random.sample({'a', 'b', 'c', 'd', 'e'}, 2)
If you also need the index, use random.randrange
from random import randrange
random_index = randrange(len(foo))
print(foo[random_index])
As of Python 3.6 you can use the secrets module, which is preferable to the random module for cryptography or security uses.
To print a random element from a list:
import secrets
foo = ['a', 'b', 'c', 'd', 'e']
print(secrets.choice(foo))
To print a random index:
print(secrets.randbelow(len(foo)))
For details, see PEP 506.
I propose a script for removing randomly picked up items off a list until it is empty:
Maintain a set and remove randomly picked up element (with choice) until list is empty.
s=set(range(1,6))
import random
while len(s)>0:
s.remove(random.choice(list(s)))
print(s)
Three runs give three different answers:
>>>
set([1, 3, 4, 5])
set([3, 4, 5])
set([3, 4])
set([4])
set([])
>>>
set([1, 2, 3, 5])
set([2, 3, 5])
set([2, 3])
set([2])
set([])
>>>
set([1, 2, 3, 5])
set([1, 2, 3])
set([1, 2])
set([1])
set([])
foo = ['a', 'b', 'c', 'd', 'e']
number_of_samples = 1
In Python 2:
random_items = random.sample(population=foo, k=number_of_samples)
In Python 3:
random_items = random.choices(population=foo, k=number_of_samples)
NumPy solution: numpy.random.choice
For this question, it works the same as the accepted answer (import random; random.choice()), but I added it because the programmer may have imported NumPy already (like me)
And also there are some differences between the two methods that may concern your actual use case.
import numpy as np
np.random.choice(foo) # randomly selects a single item
For reproducibility, you can do:
np.random.seed(123)
np.random.choice(foo) # first call will always return 'c'
For samples of one or more items, returned as an array, pass the size argument:
np.random.choice(foo, 5) # sample with replacement (default)
np.random.choice(foo, 5, False) # sample without replacement
If you need the index, just use:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print int(random.random() * len(foo))
print foo[int(random.random() * len(foo))]
random.choice does the same:)
How to randomly select an item from a list?
Assume I have the following list:
foo = ['a', 'b', 'c', 'd', 'e']
What is the simplest way to retrieve an item at random from this list?
If you want close to truly random, then I suggest secrets.choice from the standard library (New in Python 3.6.):
>>> from secrets import choice # Python 3 only
>>> choice(list('abcde'))
'c'
The above is equivalent to my former recommendation, using a SystemRandom object from the random module with the choice method - available earlier in Python 2:
>>> import random # Python 2 compatible
>>> sr = random.SystemRandom()
>>> foo = list('abcde')
>>> foo
['a', 'b', 'c', 'd', 'e']
And now:
>>> sr.choice(foo)
'd'
>>> sr.choice(foo)
'e'
>>> sr.choice(foo)
'a'
>>> sr.choice(foo)
'b'
>>> sr.choice(foo)
'a'
>>> sr.choice(foo)
'c'
>>> sr.choice(foo)
'c'
If you want a deterministic pseudorandom selection, use the choice function (which is actually a bound method on a Random object):
>>> random.choice
<bound method Random.choice of <random.Random object at 0x800c1034>>
It seems random, but it's actually not, which we can see if we reseed it repeatedly:
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
A comment:
This is not about whether random.choice is truly random or not. If you fix the seed, you will get the reproducible results -- and that's what seed is designed for. You can pass a seed to SystemRandom, too. sr = random.SystemRandom(42)
Well, yes you can pass it a "seed" argument, but you'll see that the SystemRandom object simply ignores it:
def seed(self, *args, **kwds):
"Stub method. Not used for a system random number generator."
return None
I usually use the random module for working with lists and randomization:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
In short, use random.sample method
The sample method returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples.
import random
lst = ['a', 'b', 'c', 'd', 'e']
random.seed(0) # remove this line, if you want different results for each run
rand_lst = random.sample(lst,3) # 3 is the number of sample you want to retrieve
print(rand_lst)
Output:['d', 'e', 'a']
here is a running code
https://onecompiler.com/python/3xem5jjvz
This is the code with a variable that defines the random index:
import random
foo = ['a', 'b', 'c', 'd', 'e']
randomindex = random.randint(0,len(foo)-1)
print (foo[randomindex])
## print (randomindex)
This is the code without the variable:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print (foo[random.randint(0,len(foo)-1)])
And this is the code in the shortest and smartest way to do it:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
(python 2.7)
Random item selection:
import random
my_list = [1, 2, 3, 4, 5]
num_selections = 2
new_list = random.sample(my_list, num_selections)
To preserve the order of the list, you could do:
randIndex = random.sample(range(len(my_list)), n_selections)
randIndex.sort()
new_list = [my_list[i] for i in randIndex]
Duplicate of https://stackoverflow.com/a/49682832/4383027
You could just:
from random import randint
foo = ["a", "b", "c", "d", "e"]
print(foo[randint(0,4)])
This may already be an answer, but you can use random.shuffle. Example:
import random
foo = ['a', 'b', 'c', 'd', 'e']
random.shuffle(foo)
The recommended numpy way is now to use an explicit RNG:
from numpy.random import default_rng
rng = default_rng()
rng.choice(foo)
We can also do this using randint.
from random import randint
l= ['a','b','c']
def get_rand_element(l):
if l:
return l[randint(0,len(l)-1)]
else:
return None
get_rand_element(l)