Python select from a list + Examples

In this Python tutorial, you will learn about Python select from a list.

There are six methods to select from a list in Python, which is shown below:

  • Using Indexing
  • Using Slicing
  • Using Filter
  • Using List comprehension
  • Using Map function
  • Using random.choice() function

Python select from a list

In Python, selecting elements from a list is a fundamental operation that is required in many programs. There are several ways to select elements from a list in Python, including using indexing, slicing, list comprehensions, built-in functions, etc.

Method-1: Python select from a list using Indexing

This method involves accessing a specific element in a list by its index, which is the position of the element in the list. The index starts from 0 for the first element and increments by 1 for each subsequent element.

# Create a list of countries
country_list = ['USA', 'United Kingdom', 'Canada']

# Assign the first element of the list to a variable
first_item = country_list[0]

# Print the value of the variable to the console
print(first_item)

The above code creates a list of countries named “country_list” with three elements. It then assigns the first element of the list to a new variable named “first_item”. Finally, it prints the value of “first_item” to the console.

Output: USA

Read: Linked Lists in Python

Method-2: Python select from a list using Slicing

This method involves selecting a subsequence of a list by specifying a range of indices. The subsequence starts at the index specified by the first argument of the slice operator (before the colon), and ends at the index specified by the second argument (after the colon).

# Create a list of countries
country_list = ['USA', 'United Kingdom', 'Canada', 'Brazil']

# Create a new list that contains elements from index 1 up to (but not including) index 3 of the original list
sub_list = country_list[1:3]

# Print the new list to the console
print(sub_list)

The above code creates a list of countries named “country_list” with four elements.

  • It then creates a new list called “sub_list” that contains elements from index 1 up to (but not including) index 3 of the original list. Finally, it prints the new list to the console.
Output: ['United Kingdom', 'Canada']

Read: How to Reverse a List in Python

READ:  How to Split a String at Every nth Character in Python [10 Ways]

Method-3: Python select from a list using Filter

This method involves using the built-in filter() function to create a new iterator that contains only items that match a given condition. The filter() function takes two arguments: a function that tests each element of the iterator, and the iterator to be filtered.

# Create a list of numbers
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Create a new list that contains only the even numbers from the original list using a lambda function and the filter() method
even_list = list(filter(lambda x: x % 2 == 0, num_list))

# Print the new list to the console
print(even_list)

The above code creates a list of numbers named “num_list” with ten elements.

  • It then creates a new list called “even_list” that contains only the even numbers from the original list, which is achieved using a lambda function and the filter() method. Finally, it prints the new list of even numbers to the console.
Output: [2, 4, 6, 8, 10]

Read: How to find smallest number in a Python list

Method-4: Python selects from a list using list comprehension

This method involves using a concise syntax to create a new list based on an existing list. The syntax includes a for loop that iterates over the elements of the original list, with an optional condition that filters the elements based on certain criteria.

# Create a list of numbers
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Create a new list that contains only the even numbers from the original list using a list comprehension
even_list = [x for x in num_list if x % 2 == 0]

# Print the new list to the console
print(even_list)

The above code creates a list of numbers named “num_list” with ten elements.

  • It then creates a new list called “even_list” that contains only the even numbers from the original list, which is achieved using list comprehension. Finally, it prints the new list of even numbers to the console.
Output: [2, 4, 6, 8, 10]

Read: How to add string to list Python

READ:  How to Copy Elements from One List to Another in Python [6 Methods]

Method-5: Python select from a list using map function

This method involves using the built-in map() function to create a new iterator that applies a given function to each item of the list. The map() function takes two arguments: a function that performs a transformation on each element of the iterator, and the iterator to be transformed.

# Create a list of numbers
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Create a new list that contains the square of each number in the original list using a lambda function and the map() method
squared_list = list(map(lambda x: x ** 2, num_list))

# Print the new list to the console
print(squared_list)

The above code creates a list of numbers named “num_list” with ten elements.

  • It then creates a new list called “squared_list” that contains the square of each number in the original list, which is achieved using a lambda function and the map() method. Finally, it prints the new list of squared numbers to the console.
Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Read: How to Add Elements in List in Python Using For Loop

Method-6: Python select from a list using the random.choice() function

This method involves using random.choice() function from the random module to select a random item from a list. This method returns a randomly selected item from the list.

# import the random module
import random

# create a list of countries
country_list = ['USA', 'United Kingdom', 'Canada', 'Brazil']

# select a random country
random_country = random.choice(country_list)
print('A randomly selected country is:', random_country)

The above code demonstrates how to use the random module in Python to select a random element from a list of countries.

  • The third line uses the random.choice() function to select a random country from the country_list and assigns it to the variable random_country.
Output: A randomly selected country is: Canada                                              

You may also like to read the following Python tutorials.

READ:  Python program for bubble sort [3 methods]

Conclusion

In this Python tutorial, we have learned about python select from a list using the below methods:

  • Using Indexing
  • Using Slicing
  • Using Filter
  • Using List comprehension
  • Using Map function
  • Using random.choice() function