Python - Confused in list comprehension

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

This discussion focuses on understanding list comprehensions in Python 3, specifically the syntax and functionality of the expression res = [i for i in list1 if i not in list2]. The user seeks clarity on how this construct operates to filter elements from one list based on their absence in another. The explanation provided clarifies that this expression creates a new list containing elements from list1 that are not present in list2, effectively demonstrating the power of list comprehensions for concise data manipulation.

PREREQUISITES
  • Familiarity with Python 3 syntax
  • Understanding of basic list operations in Python
  • Knowledge of conditional statements in Python
  • Basic programming concepts such as loops and iterations
NEXT STEPS USEFUL FOR

Beginner to intermediate Python developers, educators teaching Python programming, and anyone looking to enhance their skills in efficient data manipulation using list comprehensions.

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