Python - Confused in list comprehension

  • Context: Python 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Confused List Python
Click For Summary

Discussion Overview

The discussion revolves around understanding list comprehensions in Python, specifically how to find the difference between two lists using this feature. Participants explore the syntax and functionality of list comprehensions, share resources for learning, and express their confusion regarding the concept.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses confusion about the syntax of list comprehensions, particularly the line "res = [i for i in list1 if i not in list2]."
  • Another participant explains the general structure of list comprehensions, stating that it allows for appending elements to a list based on a condition.
  • Some participants suggest that understanding list comprehensions requires prior knowledge of the concept, indicating that it may not be intuitive for beginners.
  • Several participants share links to resources for learning about list comprehensions, including tutorials and interactive examples.
  • One participant mentions their unsuccessful search for video resources on the topic and expresses a preference for written explanations with examples.

Areas of Agreement / Disagreement

Participants generally agree that list comprehensions can be confusing for those unfamiliar with them, and they share resources to aid understanding. However, there is no consensus on the best method for learning, as preferences for written versus video resources vary.

Contextual Notes

Some participants note that understanding list comprehensions may depend on familiarity with Python syntax and programming concepts, which could limit accessibility for beginners.

Who May Find This Useful

Individuals learning Python, especially those interested in list operations and comprehensions, may find this discussion and the shared resources beneficial.

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   Reactions: 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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K