Python Python - Confused in list comprehension

AI Thread Summary
The discussion centers around understanding list comprehensions in Python, specifically how to find the difference between two lists. The example provided demonstrates using list comprehension to create a new list that excludes elements found in a second list. The syntax used is explained as follows: for each element in the first list, if it is not present in the second list, it is included in the result. This method is highlighted as a concise way to filter lists. Additionally, participants seek resources for learning list comprehensions, with recommendations for websites like DigitalOcean and W3Schools, which offer explanations and interactive examples. There is also a request for guidance on writing LaTeX on the forum, with a link provided for assistance. Overall, the conversation emphasizes the importance of grasping the foundational concepts of list comprehensions for effective Python programming.
shivajikobardan
Messages
637
Reaction score
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
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
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.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top