Python-Confused in list comprehension-:

  • Thread starter shivajikobardan
  • Start date
  • Tags
    List
In summary: So basically the first version is just looping over list1 and checking if the value of i is in list2 but the second version is looping over list1 and checking if the value of i is not in list2.
  • #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?)
 
Technology news on Phys.org
  • #2
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.
 
  • #3
But the original code said "if i NOT in list2" and your code says "i in list2".
 

1. What is list comprehension in Python?

List comprehension is a concise and elegant way of creating lists in Python. It allows you to create a list by looping over an iterable object and applying a condition or transformation on each element.

2. How do you write a list comprehension in Python?

A list comprehension is written in the following format: [expression for item in iterable if condition]. The expression can be any operation or transformation you want to apply on the item, the iterable is the object you are looping over, and the condition is optional but allows you to filter the elements in the new list.

3. What is the purpose of using list comprehension in Python?

List comprehension offers a more efficient and concise way of creating lists compared to traditional for loops. It also allows you to perform operations and transformations on the elements while creating the list, making it a powerful tool for data manipulation.

4. Can you nest list comprehension in Python?

Yes, you can nest list comprehension in Python. This means you can use a list comprehension inside another list comprehension to create nested lists. However, it is important to keep the code readable and avoid nesting too many levels.

5. How do you avoid confusion in list comprehension in Python?

To avoid confusion in list comprehension, it is important to understand the syntax and logic behind it. Use descriptive variable names and break down complex list comprehensions into smaller parts. Also, it is recommended to practice and get familiar with list comprehension by solving different problems.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
636
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
645
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
2
Replies
43
Views
3K
Replies
1
Views
605
  • Programming and Computer Science
Replies
3
Views
240
Back
Top