Python - Confused in list comprehension

  • #1
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?)
 

Answers and Replies

  • #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
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.
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.
To write LaTeX you can look at https://www.physicsforums.com/help/latexhelp/
Thank you.
 
  • #4
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"
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.
 

Suggested for: Python - Confused in list comprehension

Replies
2
Views
454
Replies
1
Views
453
Replies
5
Views
690
Replies
1
Views
587
Replies
43
Views
2K
Replies
23
Views
948
Replies
3
Views
215
Replies
15
Views
1K
Replies
1
Views
486
Back
Top