I have a python dictionary as follows:
{'APPLE_PROVIDERS' : ["some", "provider","can","be", "null"], .... }
What I want to do is get a random sublist from the list (which is the value) of the key. Not only one item, but also a completely random sublist. Here is what I tried:
a_list = a_dict.get('APPLE_PROVIDERS', "") for item in a_list[randrange(0,len(a_list)) : randrange(0,len(a_list))]: ...do something..
This problem has two problems:
If the list is empty or if the search by query is not performed, the program crashes because randrange has (0,0) as arguments, which leads to an error
In many cases, randrange () calls generate the same number, especially if the list is small. This returns an empty list. For example, a_list [5: 5]
So what is the best way to get a random sublist with the cases described above? In addition, I do not care about the order. Everything works. I just want a completely random subscript from 0.1 ... to len (a_list) elements every time a for loop starts.
If the list can be changed in another data structure that may contain similar elements, this also works for me.
source share