use list comprehensions to eliminate strings in list that include substrings in another list - list-comprehension

I have two lists:
list_1 = ['https:/zh-cn.fb.com/siml15', 'https:/fb.com/siml29','https:/en-gb.fb.com/siml29','https:/fb.com/siml4529','https:/pt-br.fb.com/siml29']
list_2 = ['zh-cn','en-gb','es-la','fr-fr','ar-ar','pt-br','ko-kr','it-it','de-de','hi-in','ja-jp']
I need to remove items in list_1 that includes substrings in list_2
The wanted output:
['https:/fb.com/siml29','https:/fb.com/siml4529']
Is there a way to use list comprehensions twice?
[x for x in list_1 if y for y in list_2 not in x]

You can use inner comprehension and all function for this:
list_1 = ['https:/zh-cn.fb.com/siml15', 'https:/fb.com/siml29', 'https:/en-gb.fb.com/siml29', 'https:/fb.com/siml4529', 'https:/pt-br.fb.com/siml29']
list_2 = ['zh-cn', 'en-gb', 'es-la', 'fr-fr', 'ar-ar', 'pt-br', 'ko-kr', 'it-it', 'de-de', 'hi-in', 'ja-jp']
result = [x for x in list_1 if all(y not in x for y in list_2)]
print(result)
['https:/fb.com/siml29', 'https:/fb.com/siml4529']

Related

List comprehension flat list?

I have the following toy function:
def foo(a):
return [a+5]
And I am running the following code:
my_lst = [foo(x) for x in range(10) if x%2 == 0]
Getting:
[[5], [7], [9], [11], [13]]
But need:
[5,7,9,11,13]
But I want to get a plain list, not a list of lists.
How can I do it without itertools.chain.from_iterable(my_lst) but with list comprehension?
What is the best practice? itertools or list comprehension in this case?
Please advice.
I have tried:
[j[0] for j in [foo(x) for x in range(10) if x%2 == 0]]
Should I do it like this or there is a better way?
With list comprehension, it's done using two for loops:
my_lst = [subx for x in range(10) if x%2 == 0 for subx in foo(x)]
Also if you have a list of lists of one elements, you can "transpose" after the fact the list using zip:
bad_lst = [foo(x) for x in range(10) if x%2 == 0]
[my_lst] = zip(*bad_lst)
Or you can "transpose" using a list comprehension combined with list unpacking as well if you truly want a list as output and not a tuple:
bad_lst = [foo(x) for x in range(10) if x%2 == 0]
my_lst = [x for [x] in bad_lst]
alternatively to Laernes answer, you can also use itertools as :
list(itertools.chain(*[foo(x) for x in range(10) if x%2 == 0]))
or
list(itertools.chain.from_iterable([foo(x) for x in range(10) if x%2 == 0]))
More options here:
Your function should return a number, not a list:
def foo(a):
return a+5

Mistake in list comprehension

Problem Solved!
I have two python lists of integers that are randomly generated. I want to find the numbers that are common between the lists.
Using list comprehension, I've come up with this:
new_list = [a for a in list_one for b in list_two if a == b and a not in new_list]
However, this returns an empty list. I have a working program using loops that works perfectly:
for a in list_one:
for b in list_two:
if a == b and a not in new_list:
new_list.append(a)
What mistakes have I made converting it to a list comprehension?
This is a simple approach with sets,
list_1 = [1,2,3,4,5]
list_2 = [5,4,7,7,5,1,2,8,9]
new_list = list(set(list_1) & set(list_2))
print(new_list)
It uses the set intersection has a number of benefits compared to a list comprehension:
The input lists do not need to be sorted (though you can sort them by using sorted() function.
The input lists can have different lengths.
Presence of duplicates is taken care of automatically (sets don't admit those).
I do not think list comprehension is required for intersection of 2 lists.
Anyways you can modify your code to-
list_one = [1,3,4,5]
list_two = [1,5]
new_list = []
new_list = [a for a in list_one for b in list_two if a == b and a not in new_list]
print(new_list)
you can also do-
list_one = [1,3,4,5]
list_two = [1,5]
new_list = []
new_list = [a for a in list_one if a in list_two and a not in new_list]
print(new_list)
You can convert it into set and use set1.intersection(set2).
list_one = [1,3,4,5]
list_two = [1,5]
new_list = []
new_list = list(set(list_one).intersection(set(list_two)))
print(new_list)
You can't reference the same list inside of a list comprehension.
Thanks #Keith from the comments of the OP for pointing it out.

How to multiply 2 input lists in python

Please help me understand how to code the following task in Python using input
Programming challenge description:
Write a short Python program that takes two arrays a and b of length n
storing int values, and returns the dot product of a and b. That is, it returns
an array c of length n such that c[i] = a[i] · b[i], for i = 0,...,n−1.
Test Input:
List1's input ==> 1 2 3
List2's input ==> 2 3 4
Expected Output: 2 6 12
Note that the dot product is defined in mathematics to be the sum of the elements of the vector c you want to build.
That said, here is a possibiliy using zip:
c = [x * y for x, y in zip(a, b)]
And the mathematical dot product would be:
sum(x * y for x, y in zip(a, b))
If the lists are read from the keyboard, they will be read as string, you have to convert them before applying the code above.
For instance:
a = [int(s) for s in input().split(",")]
b = [int(s) for s in input().split(",")]
c = [x * y for x, y in zip(a, b)]
Using for loops and appending
list_c = []
for a, b in zip(list_a, list_b):
list_c.append(a*b)
And now the same, but in the more compact list comprehension syntax
list_c = [a*b for a, b in zip(list_a, list_b)]
From iPython
>>> list_a = [1, 2, 3]
>>> list_b = [2, 3, 4]
>>> list_c = [a*b for a, b in zip(list_a, list_b)]
>>> list_c
[2, 6, 12]
The zip function packs the lists together, element-by-element:
>>> list(zip(list_a, list_b))
[(1, 2), (2, 3), (3, 4)]
And we use tuple unpacking to access the elements of each tuple.
From fetching the input and using map & lambda functions to provide the result. If you may want to print the result with spaces between (not as list), use the last line
list1, list2 = [], []
list1 = list(map(int, input().rstrip().split()))
list2 = list(map(int, input().rstrip().split()))
result_list = list(map(lambda x,y : x*y, list1, list2))
print(*result_list)
I came out with two solutions. Both or them are the ones that are expected in a Python introductory course:
#OPTION 1: We use the concatenation operator between lists.
def dot_product_noappend(list_a, list_b):
list_c = []
for i in range(len(list_a)):
list_c = list_c + [list_a[i]*list_b[i]]
return list_c
print(dot_product_noappend([1,2,3],[4,5,6])) #FUNCTION CALL TO SEE RESULT ON SCREEN
#OPTION 2: we use the append method
def dot_product_append(list_a, list_b):
list_c = []
for i in range(len(list_a)):
list_c.append(list_a[i]*list_b[i])
return list_c
print(dot_product_append([1,2,3],[4,5,6])) #FUNCTION CALL TO SEE RESULT ON SCREEN
Just note that the first method requires that you cast the product of integers to be a list before you can concatenate it to list_c. You do that by using braces ([[list_a[i]*list_b[i]] instead of list_a[i]*list_b[i]). Also note that braces are not necessary in the last method, because the append method does not require to pass a list as parameter.
I have added the two function calls with the values you provided, for you to see that it returns the correct result. Choose whatever function you like the most.

How to pair two list in python

I think someone may have asked this question already, but for some reasons I just cannot come out good key words to find the answers for it.
I have two separate lists, and I could like to pair them.
list_a = [[1,2] [3,4]]
list_b = [[5],[6]]
I would like to generate:
list_c = [[[1,2],[5]],[[3,4],[6]]]
Thank you for your help
The following code should do the trick!
list_c = [[x, y] for x, y in zip(list_a, list_b)]
The zip function acts to 'pair' the list elements together, while the list comprehension builds the new list.
If you want to append them to a new list, this is what you want:
list_a = [[1,2], [3,4]]
list_b = [[5],[6]]
list_res = []
for a, b in zip(list_a, list_b):
list_res.append([a, b])
>list_res
>[[[1, 2], [5]], [[3, 4], [6]]]

How to concatenate 2 list strings using list comprehension

I have 2 lists as below:
list1 = ['A','B']
list2 = ['C','D']
The required output should be [['AC','BC'], ['AD','BD']] using List comprehension.
Using two for loops as a list comprehension
list1= ['A','B']
list2= ['C','D']
b=[[l+p for l in list1] for p in list2]
print(b)

Resources