Python has a language feature called List Comprehensions that is perfectly suited to making this sort of thing extremely easy. The following statement does exactly what you want and stores the result in l3
:
l3 = [x for x in l1 if x not in l2]
l3
will contain [1, 6]
.
More Related Contents:
- how to unpack a tuple in this form (‘abc’, [20, 20])? [closed]
- Getting a map() to return a list in Python 3.x
- How do I merge a list of dicts into a single dict?
- Splitting a list into N parts of approximately equal length
- How to do a recursive sub-folder search and return files in a list?
- List of lists into numpy array
- Replace values in list using Python [duplicate]
- Split a list into parts based on a set of indexes in Python
- Is there a better way to iterate over two lists, getting one element from each list for each iteration? [duplicate]
- How do I iterate through two lists in parallel?
- Python – How to check list monotonicity
- Why does appending to one list also append to all other lists in my list of lists? [duplicate]
- How to return a subset of a list that matches a condition [duplicate]
- Using an index to get an item
- Some built-in to pad a list in python
- Remove duplicates and original from list
- Python – sum values in dictionary
- How do I split a string into a list of words?
- Sliding window maximum in O(n) time
- Convert list into a pandas dataframe
- Why do these list methods (append, sort, extend, remove, clear, reverse) return None rather than the resulting list?
- Sorting list according to corresponding values from a parallel list [duplicate]
- Appending to list in Python dictionary [duplicate]
- Does Python have an immutable list?
- Swap values in a tuple/list inside a list in python?
- Sort list while pushing None values to the end
- How to combine elements from two lists into a third?
- smartest way to join two lists into a formatted string
- Sum list of list elements in python like sql group by [duplicate]
- Python : how to append new elements in a list of list?