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)
Related
I have the following dictionary:
d = {}
d[1] = 'a'
d[2] = 'b'
d[3] = 'c'
d[4] = 'd'
I'd like to perform a reverse dictionary lookup for each character in a string:
input_string = "bad"
I get different results when I do this in a list comprehension as opposed to a nested for loop, and I don't understand why. As I understand, the list comprehension and the nested for loop should yield identical results. The list comprehension yields a list whose results are not in the order I would expect. My desired result here is that which is provided by the nested for loop, however I prefer to use the list comprehension to accomplish that. Perhaps this has something to do with python dictionary order of which I am unaware?
result1 = [key for key, value in d.items() for i in input_string if i == value]
print(result1)
> [1, 2, 4]
result2 = list()
for i in input_string:
for key, value in d.items():
if i == value:
result2.append(key)
print(result2)
> [2, 1, 4]
In order to mimic the traditional loop, the outer loop should be over input_string and the inner loop should be over d in the list comprehension:
out = [k for i in input_string for k,v in d.items() if i==v]
Output:
[2, 1, 4]
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.
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']
I have a list of lists like this
list1 = [['I am a student'], ['I come from China'], ['I study computer science']]
len(list1) = 3
Now I would like to convert it into a list of string like this
list2 = ['I', 'am', 'a', 'student','I', 'come', 'from', 'China', 'I','study','computer','science']
len(list2) = 12
I am aware that I could conversion in this way
new_list = [','.join(x) for x in list1]
But it returns
['I,am,a,student','I,come,from,China','I,study,computer,science']
len(new_list) = 3
I also tried this
new_list = [''.join(x for x in list1)]
but it gives the following error
TypeError: sequence item 0: expected str instance, list found
How can I extract each word in the sublist of list1 and convert it into a list of string? I'm using python 3 in windows 7.
Following your edit, I think the most transparent approach is now the one that was adopted by another answer (an answer which has since been deleted, I think). I've added some whitespace to make it easier to understand what's going on:
list1 = [['I am a student'], ['I come from China'], ['I study computer science']]
list2 = [
word
for sublist in list1
for sentence in sublist
for word in sentence.split()
]
print(list2)
Prints:
['I', 'am', 'a', 'student', 'I', 'come', 'from', 'China', 'I', 'study', 'computer', 'science']
Given a list of lists where each sublist contain strings this could be solved using jez's strategy like:
list2 = ' '.join([' '.join(strings) for strings in list1]).split()
Where the list comprehension transforms list1 to a list of strings:
>>> [' '.join(strings) for strings in list1]
['I am a student', 'I come from China', 'I study computer science']
The join will then create a string from the strings and split will create a list split on spaces.
If the sublists only contain single strings, you could simplify the list comprehension:
list2 = ' '.join([l[0] for l in list1]).split()
I have 3 series, each has equivalent length, I converted them to 3 list. I want to concatenate the strings in the lists at the same index, and put the concatenated strings in another list. How to do that? e.g. list1[0] + list2[0] + list3[0] for each index n.
You can use zip() and a list comprehension:
>>> l1 = ["a", "b", "c"]
>>> l2 = ["1", "2", "3"]
>>> l3 = ["!", "?", "."]
>>> [''.join(item) for item in zip(l1, l2, l3)]
['a1!', 'b2?', 'c3.']
what if the l1, l2, l3 are in a list l, and I don't know how many elements in l, how to do the concatenation
In this case, you can just unpack the list with sublist to the zip() function arguments:
>>> l = [l1, l2, l3]
>>> [''.join(item) for item in zip(*l)]
['a1!', 'b2?', 'c3.']