Understand Javascript Lists

| | | |

👻 See our latest reviews to choose the best laptop for Machine Learning and Deep learning tasks!

Understanding a Python list is a way to build a list based on an existing list. List understandings are commonly used to filter items from a list or to modify values ‚Äã‚Äãin an existing list. List understandings are in square brackets.

When working with lists, you can create a list based on the contents of an existing sequence. For example, you can create a list based on a sequence of characters. Or you might want a list that doubles the contents of another list by two.

is where list comprehension comes in. This tutorial will explore, with examples, the basics of Python lists and how to use list comprehension

Python lists: . An updated

List data structure allows you to store collections of items in Python. Lists are commonly used when you want to work with multiple values ‚Äã‚Äãthat are related in some way.

For example, you can use a list to store all the flavors of ice cream sold in an ice cream store. Or you can use a list to store a list of wine club member phone numbers

Here is an example of a list in Python.

Now that we’ve gone through the basics of lists, we can start talking about how to use list comprehension .

understanding Python lists

Understanding the Python list creates a new list of the contents of another list. You can use a including list to duplicate a list or to change the contents of an existing list to a new list. You can also transfer the contents of another iterable into a list with an understanding of the list

You can specify filters so that your new list only includes certain values. For example, you can create a new list from a list of numbers. Your new list cannot include a number greater than 250.

The syntax for understanding a list in Python is as follows:

This syntax is similar to a Python for declaration . But the statement is on one line. To distinguish between a for statement, a list comprehension is enclosed in square brackets

There are three parts in the previous syntax:

You can use a list including a Python declaration " if ... else inside:

This understanding adds items to the list only if they meet the specified conditions.

Using List Understanding allows you to create a new list of one without defining for complete statements. for stat elements take at least two lines of code, while you can write a list comprehension on one line.

Some people say that understanding the list is more Python code. This is because they are more efficient than using a short for statement.

Example of understanding the Python list

Suppose you want to create a list of all the chicken pizzas that we sell at our store. We will carry all chicken pizzas to the Chicken category in our menu. To do this, we could filter our pizzas using the for instructions. Or, we could filter our list using a list comprehension .

We could use a including list to generate a new list of pizzas whose name contains Chicken based on our list of existing pizzas. We could do it using this code:

Our understanding returns the following output list:

in our code, we first define a list of pizzas in our menu. Our pizzas are stored in the Python variables called pizzas . Next, we’ll use a list including to create a new list of pizzas whose names contain Chicken

Our understanding of the list is made up of the following:.

  • p :. this is the value to add to our list
  • p in the pizzas : this flow in each pizza in our "pizzas" . list
  • if "Pollo" in p : check if each pizza contains "Pollo". If it returns True, the value stored in "p" is added to our list

Our understanding that lists only need one line of code, whereas in our next example we need to use three lines of code with for declaration to make it work . Our next example shows how comprehension lists helps write cleaner code .

Without comprehension lists

Let’s go dos to the chicken pizza example. Without help From an understanding of the list, if we wanted to generate a list of chicken pizzas on our menu, we would use this code:

Our code returns:

First , we will define two lists. A list stores a list of pizzas sold at our store. The other listing is a list of all the chicken pizzas we sell. The list of chicken pizzas we sell is initially empty.

We use a "for" statement to review every pizza on our pizza list. Next, we’ll check if each pizza contains the word Chicken. If a pizza contains the word Chicken, we’ll add that pizza to our list of chicken pizzas. In this case, three pizzas contain Chicken and all three of those pizzas are added to our pizza_pollo list.

. There is a more efficient way to write this code:. Using list understandings

List understandings Using if ... else

You can also use an if ... else. statement with an understanding of the list

Before used if < / em> to add pizza to our chicken pizza list. We only added a pizza if the name of the pizza included the term Chicken. But suppose we make a list of which pizzas are vegetarian.

We want to add " meat" to a list if the name of a pizza contains Chicken and Vegetarian if a pizza name does not contain Chicken

This can be done using an understanding of the list:.

Nos code returns:

We describe our code. First, we will declare a list of pizzas called pizzas. Next, we use a list including to evaluate whether the name of a pizza contains Chicken or not.

If the name of a pizza contains Chicken, meat value is added to our is_veggie. Otherwise, the Vegetarian value is added.

As you can see, the first value in our is_veggie is Meat, because its corresponding value in the pizzas > is Chicken. But our next value is Vegetarian, because its corresponding value in the pizzas is Margherita, which doesn’t contain Chicken .