Python - Confused in list comprehension

In summary, list comprehensions are a way of writing code that allows you to perform a task on all the elements of a list, without having to write individual code for each one.
  • #1
shivajikobardan
674
54
I want to find difference between two list. I came across sth called list comprehension. I can't tell you how confused I am with this.

Code:
# Python 3 code to demonstrate
# to remove elements present in other list
# using list comprehension

# initializing list1
list1 = [1, 3, 4, 6, 7]

# initializing list2
list2 = [3, 6]

# printing list1
print("The list1 is : " + str(list1))

# printing list2
print("The list2 is : " + str(list2))

# using list comprehension to perform task
res = [i for i in list1 if i not in list2]

# printing result
print("The list after performing remove operation is : " + str(res))

My confusion lies here-:

Code:
res = [i for i in list1 if i not in list2]

What is happening here? I can't understand a single word. How to understand this please guide me.

Also can you guide a link to how to write latex in this site(how to write math? I am familiar with stackexchange mathjax can I use that here?)
 
Technology news on Phys.org
  • #2
Essentially list comprehensions are written in the form: [A for i in B if C], which means, for each element of B, if C is True, append A to the list. It is essentially equivalent to the code
Python:
list = []  # We start with an empty list
for i in B:  # For each element of B
    if C:  # If C is True
        list.append(A)  # We append A to the list
In your case, for B is the first list, so for each element, we check whether (i not in list2) is True or False, if it is True (i.e. if the element doesn't belong to the second list) then we append A (in this case it is i itself) to the list. The code produces a list with all the elements of list1 that are not on list2.

Anyway, if you don't know what list comprehensions are, it makes no sense that you try to understand "# code to demonstrate X using list comprehension."

To write LaTeX you can look at https://www.physicsforums.com/help/latexhelp/
 
  • Like
Likes shivajikobardan
  • #3
Gaussian97 said:
Essentially list comprehensions are written in the form: [A for i in B if C], which means, for each element of B, if C is True, append A to the list. It is essentially equivalent to the code
Python:
list = []  # We start with an empty list
for i in B:  # For each element of B
    if C:  # If C is True
        list.append(A)  # We append A to the list
In your case, for B is the first list, so for each element, we check whether (i not in list2) is True or False, if it is True (i.e. if the element doesn't belong to the second list) then we append A (in this case it is i itself) to the list. The code produces a list with all the elements of list1 that are not on list2.
Thanks a lot. Clears my point.
Gaussian97 said:
Anyway, if you don't know what list comprehensions are, it makes no sense that you try to understand "# code to demonstrate X using list comprehension."
Exactly. can you recommend some easier ways to learn this stuff? I searched in youtube and didn't found about this. I understood this, I will again try reading and watching about it. Thank you for telling this.
Gaussian97 said:
To write LaTeX you can look at https://www.physicsforums.com/help/latexhelp/
Thank you.
 
  • #4
shivajikobardan said:
Exactly. can you recommend some easier ways to learn this stuff?
Here's one link of many I found: https://www.digitalocean.com/community/tutorials/understanding-list-comprehensions-in-python-3
The link above had several examples

My web search was for "list comprehension in Python 3"
shivajikobardan said:
I searched in youtube and didn't found about this.
IMO, it's better to read explanations of a given topic, some with working examples that you can try out directly on the web page, than it is to watch youtube videos about it.
 
  • #5
  • Like
Likes Mark44

FAQ: Python - Confused in list comprehension

1. What is list comprehension in Python?

List comprehension is a concise and efficient way to create a new list from an existing list in Python. It allows you to iterate through a list, perform operations on each element, and return a new list in a single line of code.

2. How is list comprehension different from a for loop?

List comprehension is more compact and efficient compared to a traditional for loop. It also eliminates the need for creating an empty list and appending elements to it. Additionally, list comprehension can be used to perform conditional filtering, which can be cumbersome to do with a for loop.

3. What is the syntax for list comprehension?

The general syntax for list comprehension in Python is [expression for item in list if condition]. The expression is the operation to be performed on each element, the item is the element from the original list, and the condition is optional and used for conditional filtering.

4. Can list comprehension be nested?

Yes, list comprehension can be nested to handle more complex operations. However, it is important to keep the code readable and avoid excessive nesting.

5. What are the advantages of using list comprehension?

List comprehension offers several benefits, including a more concise and readable code, improved performance, and the ability to combine multiple operations in a single line. It also promotes the use of functional programming over imperative programming, which can lead to more efficient and bug-free code.

Similar threads

Replies
2
Views
818
Replies
3
Views
1K
Replies
3
Views
964
Replies
5
Views
2K
Replies
19
Views
2K
Replies
3
Views
678
Replies
5
Views
1K
Replies
43
Views
3K
Back
Top