How can I Randomly Select an Item from a List | Python List Problems with Answers

In this post we are going to learn about python list – how to select an element randomly, basic concepts of list, how to iterate the list , size of the list, adding element into a list along with examples and python programs.

List in Python

Lists are just like dynamically sized arrays, Just like ArrayList in Java and C++. And we can say it is a collection of things. We use [ ] square brackets where we store the data, It is a sequenced data type where we store a collection of data just like Tuples and Strings Lets understand with an example.

Example on List

it = [“Prakhar”, “Yadav”, “Kanha”]

print(it)

OUTPUT:

Moreover, a list can store any kind of data, in fact, a single list may contain different datatypes, for example, Integer, float, decimals, Strings, and even Objects. Which makes the list the most powerful tool. More Lists are mutable which means we can edit, add, and delete their data at any point in time. It may also contain duplicate, distinct or multi-value elements in it.

There are a number of operations that we can perform on a list for example Deleting, Editing, removing, adding in a different manner, selecting and displaying, Slicing, and storing objects in it. here we will focus on Displaying the elements stored in the list in all the possible ways like from front, back, in between and randomly any elements.

  Accessing the elements of the list

We can access any given elements from the list directly just by providing the index number. So makes the list an easy-to-access elements datatype.  For example:-

  Negative Indexing

Negative indexing is also supported in python List where the -ve(Negative) sign indicates that we need to start the indexing from the back which is the last element in the list that has an index number of -1.

Note- negative indexing starts from -1, not from 0., Whereas indexing from the front starts from 0.
For example

List = [1, 2, 'Prakhar', 4, 'Kanha', 6, 'Yadav']
print(List[-1])
print(List[-3])
print(list[0])

 

 Size of the list

we can get the size of the list by a simple function that is the length by simply len(list).

List2 = [10, 20, 14]
print(len(List2))

 

Adding elements in the List

As it has already been conveyed the list is mutable so we can add, Delete, or edit any of the elements in the list. by using the function append () we can add new elements to the existing list for example:-

List1 = []
print("blank List: ")
print(List1)
 
# Adding of Elements
# in the List1
List1.append(34)
List1.append("Ram")
List1.append(23.2)
print1("\nList after Addition of Three elements: ")
print(List1)

We can add the elements in the list by the insert () method and also by specifying the position or we can say edit any existing element and rewrite it As,

List = [1,2,3,4]
print("Initial List: ")
print(List)
 
# Addition of Element at
# specific Position
# (using Insert Method)
List.insert(3, 12)
List.insert(0, 'Geeks')
print("\nList after performing Insert Operation: ")
print(List)

We can  also add new elements n the list by extend( ) method

List = [1, 2, 3, 4]
print("Initial List: ")
print(List)
 
# Addition of multiple elements
# to the List at the end
# (using Extend Method)
List.extend([8, 'Geeks', 'Always'])
print("\nList after performing Extend Operation: ")
print(List)

you can read more about list from this link :  Linked List Program in Python – Create Linked List, Add Node, Iterrate and Print Data

Random() Function

we have an option in python to get the random element of the list by just using a pre-defined function that is being used a lot in machine learning also when we need to generate any random number for checking the list.
It is being used at a number of places for example when we need to check the list what exactly that list might be storing that we can use the random() function to get any random element and could get an idea about the whole of the data of the List to instate of checking as a whole list.

For using the random function we need to import the library of random to use it.

import random as rn

val = ["Ram", "Shyam", "Rakesh", "Prakhar", "Tom","Harry"]
print(rn.choice(val))

 

OUTPUT:

Note:- Remember each time it will give a different answer as a result as it will select any element randomly each time.

Learn More about the Random Function of a list in python:- https://stackoverflow.com/questions/306400/how-can-i-randomly-select-an-item-from-a-list

More tutorials from Python: Python

Leave a Comment

%d bloggers like this: