Python Python-Confused in list comprehension-:

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    List
AI Thread Summary
The discussion centers on understanding list comprehension in Python, specifically how to find the difference between two lists. The example provided demonstrates how to remove elements from one list (list1) that are present in another list (list2) using list comprehension. The key line of code, "res = [i for i in list1 if i not in list2]," creates a new list (res) containing only the elements from list1 that are not found in list2. This is equivalent to a traditional for loop that checks each element of list1 against list2 and appends the non-matching elements to res. The explanation clarifies that the use of "if i not in list2" is crucial for achieving the desired outcome of filtering out elements. Additionally, there is a request for guidance on writing mathematical expressions using LaTeX on the forum. Overall, the discussion highlights the utility of list comprehension for concise and efficient list manipulation in Python.
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
List comprehension takes some time to get used to but it's a very nice way to do a number of operations in one line and store the result as a new list.Let's simplify the comprehension slightly to piece together what is going on.

res = [i for i in list1]

This is the same as doing this: for i in list1: It just loops over list1 and i is the value of the current iteration. Ok now let's go back to the full example.

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

This is the same as doing this.
Code:
res = []
for i in list1:
    if i in list2:
        res.append(i)

The first version that uses list comprehension loops over list1 using i, then during each loop it checks "Is i in list2?" then it stores all of the results to another list which we call res.
 
But the original code said "if i NOT in list2" and your code says "i in list2".
 
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