Python select from a list + Examples

In this python tutorial, you will learn about python select from a list. Also, we will check:

  • Python select from the list
  • Python select from the list randomly
  • Python select from a list of dictionaries
  • Python select value from list of tuples
  • Python randomly select the same element from the list every time
  • Python select from list with different probability
  • Python select element from list with different probability using NumPy
  • Python select from list user input

Python select from the list

  • To select elements from a Python list, we will use list.append().
  • We will create a list of indices to be accessed and the loop is used to iterate through this index list to access the specified element.
  • And then we add these elements to the new list using an index.
my_list = [10, 11, 12, 13, 14]
i = [1, 4]
element = []
for index in i:
    element.append(my_list[index])
print(element)

In this output, we can see that the specified index elements from the list are given and we will get the respective value in the new list.

You can refer to the below screenshot Python select from the list.

Python select from the list
Python select from the list

Python select from the list randomly

To select from the Python list randomly, we have the built-in module random which is used to work with the random data from the list. The sample() is the inbuilt method of the random module which is used for random selection.

Example:

import random
my_list = [10, 20, 30, 40, 50]
u_list = random.sample(my_list, 3)
print(u_list)

After writing the above code, Once you will print “ u_list ” then the output will appear as “[50, 10, 40]” which is the random value from the list.

You can refer to the below screenshot python select from the list randomly.

Python select from the list randomly
Python select from the list randomly

Python select from list of dictionaries

In Python, sometimes we have to select the value from a list of dictionaries and we need to initialize the list of a Python dictionary. The list comprehension is used to select the value from a list of a dictionary according to the condition.

Example:

my_list = [{'x':2, 'y':4, 'z':8}, {'x':5, 'y':8, 'z':18},
            {'x':48, 'y':5, 'z':15}, {'x':12, 'y':21, 'z':2}]
print ("n_list", str(my_list))
s = [d for d in my_list if d['z'] > 12]
print ("selected_ele", str(s))

After writing the above code, Once you will print “ str(s) ” then the output will appear as “selected_ele [{‘x’: 5, ‘y’: 8, ‘z’:18}, {‘x’:48, ‘y’:5, ‘z’:15}] which return the selected element from list of dictionaries.

You can refer to the below screenshot Python select from list of dictionaries.

Python select from list of dictionaries
Python select from list of dictionaries

Read: Python NumPy linspace + Examples

Python select value from list of tuples

Selecting a value from a list of tuples in Python, we have to use list comprehension and for loop for getting the matching pattern according to the condition.

Example:

my_list = [(12,5),(16,6),(23,12),(30,15)]
s = [money for (money,id) in my_list if id == 15]
print(s)

After writing the above code, Once you will print “s” then the output will appear as a “ [30] ”. Here, for loop is used to get the value according to the given condition from the list of tuples.

You can refer to the below screenshot Python select value from list of tuples.

Python select value from list of tuples
Python select value from list of tuples

Python randomly selects the same element from the list every time

Randomly selecting the same element from the list is possible. So, we have the random.seed() and random.choice() function which will provide the same element every time.

Example:

import random 
f_list = [20.5. 15.5, 18.5, 42.5, 35.4, 56.4]
random.seed(4)
r_item = random.choice(f_list)
print("The random element", r_item)
random.seed(4)
r_item = random.choice(f_list)
print("The random element", r_item)

In this output, we can see that the randomly same element from the list is selected every time by using the random.seed() which initialize the random number generators.

You can refer to the below screenshot python randomly selects the same element from the list every time.

Python randomly selects the same element from the list every time
Python randomly selects the same element from the list every time

Python select from list with probability

To select from a list with probability we have the random.choice() method. It will return any random value from the given list.

Example:

import random
my_list = [10, 20, 30, 40, 50]
r = random.choice(my_list)
print(r)

After writing the above code, Once you will print “ r ” then the output will appear as “ 10 “. Here, random.choice() is used which will select the random element from the list with probability.

You can refer to the below screenshot python select from list with probability.

Python select from list with probability
Python select from list with probability

Read: Python NumPy concatenate

Python select element from list with different probability using NumPy

To select an element from the list we have to import numpy, and then we will create one list. Also, we will use numpy.random.choice() method for choosing elements from the list with a different probability.

Example:

import numpy as np
n_list = [51, 52, 53, 54, 55]
num = np.random.choice(n_list)
print(num)

After writing the above code, Once you will print “num” then the output will appear as a “ 53 ”. Here, np.random.choice(n_list) is used for choosing the element from the list with a different probability.

You can refer to the below screenshot python select element from list with different probability using NumPy

Python select element from list with different probability using NumPy
Python select element from list with different probability using NumPy

You may like the following tutorials:

In this Python tutorial, we have learned about the python select from a list. Also, We covered these below topics:

  • Python select from the list
  • Python select from the list randomly
  • Python select from a list of dictionaries
  • Python select value from list of tuples
  • Python randomly select the same element from the list every time
  • Python select from list with different probability
  • Python select element from list with different probability using NumPy
  • Python select from list user input