# Can items of a list be accessed in any order?

### Question

This exercise asks to print a particular index of a list. Can list elements be accessed in any order?

Yes, list elements can be accessed in any order so long as the index used is valid. Valid indexes for a list are values from 0 through `len() - 1` of the list. The following example shows a list whose items are randomly accessed using the `randint()` function. As long as the value returned by `randint()` is between 0 and `len() -1` for the list, there is no issue accessing the list items. If the index used is greater than `len() -1` for the list, then an `IndexError` will occur.

The exception to this is the use of a negative index. A negative index will return an element from the end of the list without needing to know how many items are in the list.

``````from random import randint

elements = ['Hydrogen', 'Helium', 'Carbon', 'Oxygen', 'Nitrogen']

for count in range(10):
index = randint(0, len(elements) - 1)
print(elements[index])
``````
13 Likes

The above example may print repeated items. Below will generate a random list…

``````>>> from random import randrange
>>> randlist = []
>>> while len(randlist) < len(elements):
x = randrange(0, len(elements))
if elements[x] not in randlist:
randlist.append(elements[x])

>>> randlist
['Helium', 'Nitrogen', 'Hydrogen', 'Carbon', 'Oxygen']
>>>
``````
21 Likes

For this example, can we use randint() instead of randrange()?

Yes, with a slight change of arguments. randint(a,b) has inclusive a and b, whereas randrange(a,b) behaves like range(), that is, a is inclusive, but b is one greater than the desired upper limit. From the docs:

``````random.randint(a, b)
Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).
``````
3 Likes

The use case will dictate which is most preferred. If working with list, tuple or string indices `randrange` is convenient since we can use it exactly as we would with `range`. The default starting value is `0` which like range, can be omitted.

`randint` has two compulsory positional arguments so cannot be written in the default form like `randrange`. Both functions take `int` type arguments so cannot be used with floats.

1 Like

whats the role of -1 after the len(elements) ?? because it keeps printing a list if i erase it but the list printed has a different range each time, and also errors occur.

and why it throws an error if i erase the -1 and write 0 instead?

What information do you have and what information do you need to give to that function? If the relation between those two is off by one, then how would you go from one to the other?
You always need to consider that regardless of whether they match up or not, because it’s up to the programmer to get things to match up, that’s what a program is, isn’t it? Describing the relation between input and output.

Refer to the posts above that describe the two functions, randint and randrange. Note that since the second bound in randint is inclusive, it will result in a possible outcome that attempts to access an index that does not exist (out of range). That’s why we must subtract 1. In randrange (like range) the last value is already one less than the length.

2 Likes