complex list comprehension syntax - python

Just to know if it is possible to write this in a line comprehension and if it is, how to write it.
lst1 = [0,1,2,3,4,5,6,7,8,9,10]
lst2 = [[0,4],[1,5],[2,6],[3,7]]
list3 = []
list4 = []
list5 = []
for l in lst1:
for k in lst2:
if l == k[0]:
list3.append(k)
elif l==k[1]:
list4.append(k)
else:
list5.append(l)
print('lst1',lst1)
print('lst2',lst2)
print('list3',list3)
print('list4',list4)
print('list5',list5)

You can generally turn a nested for loop that builds a list into a list comprehension by using the same exact for ... in statements in the same order:
list3 = [k for l in lst1 for k in lst2 if l == k[0]]
list4 = [k for l in lst1 for k in lst2 if l == k[1]]
list5 = [l for l in lst1 for k in lst2 if l not in k]

Related

How to Check if elements of two lists are equal to the 3rd list [duplicate]

This question already has answers here:
Common elements comparison between 2 lists
(14 answers)
Closed last year.
I have this code:
list1 = [1,2,3]
list2 = [3,4,5]
list3 = []
for i, j in zip(list1,list2):
if i==j:
list3 = i,j
How can I make list3 store elements that are the same between list1 and list2?
An easy way using Python's set intersection:
list1 = [1,2,3]
list2 = [3,4,5]
list3 = list(set(list1).intersection(list2))
print(list3)
or using a for loop:
list1 = [1,2,3]
list2 = [3,4,5]
list3 = []
for i in list1:
if i in list2:
list3.append(i)
print(list3)

Comparing lists with lists of lists

How can I check if a string of list2 is insinde a string of a list of list1?
For example:
list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
A simple solution using loops
list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
for x in range(len(list1)):
for y in range(len(list2)):
if str(list1[x]).find(str(list2[y])) == -1 :
print("NO")
else :
print("YES")
Function returns true if element of list2 exists in list1
def my_find(list1, list2):
for cur in list2:
for cur_list in list1:
if cur in cur_list:
return True
return False
here you go:
list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
res = [[l in e[0] for l in list2] for e in list1]

how to attach two strings from different lists with respect to the range of the list

I want to attach two strings from different lists .
list1 = ['dir1/dir2/c/', 'dir1/dir2/java/', 'dir1/dir2/python/']
list2 = [['c1', 'c2', 'c3'], ['java1', 'java2', 'java3'],['python1','python2']]
For every item in list1, I need to join the items in list2.
output:
dir1/dir2/c/c1
dir1/dir2/c/c2
dir1/dir2/c/c3
dir1/dir2/java/java1
dir1/dir2/java/java2
dir1/dir2/java/java3
dir1/dir2/python/python1
dir1/dir2/python/python2
Code:
for i in list1:
for j in list2:
for k in j:
print(i+'/'+k)
You can zip the two lists:
for d, l in zip(list1, list2):
for s in l:
print(d + s)
This outputs:
dir1/dir2/c/c1
dir1/dir2/c/c2
dir1/dir2/c/c3
dir1/dir2/java/java1
dir1/dir2/java/java2
dir1/dir2/java/java3
dir1/dir2/python/python1
dir1/dir2/python/python2
Use enumerate to get the index of the list you're processing. This way, you can choose which inner-list to iterate in your inner for-loop.
list1 = ['dir1/dir2/c/', 'dir1/dir2/java/','dir1/dir2/python/']
list2 = [['c1','c2','c3'],['java1','java2','java3'],['python1','python2']]
for i, li in enumerate(list1):
for j in list2[i]:
print(li+j)
Outputs:
dir1/dir2/c/c1
dir1/dir2/c/c2
dir1/dir2/c/c3
dir1/dir2/java/java1
dir1/dir2/java/java2
dir1/dir2/java/java3
dir1/dir2/python/python1
dir1/dir2/python/python2

Filter list of lists based on another list of lists

I'm trying to filter a list of lists based on the values of another list of lists in Python.
For example, I have:
list1 = [[0,1,2],[0,2,3]]
list2 = [['a','b','c'],['a','b','b']]
and want to filter list1 so that it only contains values at the same indexes as 'a' in list2. So my desired result is
filteredList_a = [[0],[0]]
Similarly, filtering list1 to have only values at the same indexes as 'b' in list2 would give
filteredList_b = [[1],[2,3]]
I know how to do this for a single list,
>>> list1 = [0,1,2]
>>> list2 = ['a','a','b']
>>> [i for index, i in enumerate(list1) if list2[index] == 'a']
[0,1]
Here's an extension of your list comprehension approach with nested list comprehensions and zip to avoid indexes:
def filter_by(a, b, target):
return [[i for i, j in zip(x, y) if j == target] for x, y in zip(a, b)]
list1 = [[0,1,2],[0,2,3]]
list2 = [['a','b','c'],['a','b','b']]
print(filter_by(list1, list2, 'a'))
print(filter_by(list1, list2, 'b'))
Output:
[[0], [0]]
[[1], [2, 3]]
Try it!

repeat the elements in a list

so this is my list:
my_list = [['a','b','c'],['d','e','f']]
I want to have the output that times 2 times each:
final_list = [['a','a','b','b','c','c'],['d','d','e','e','f','f']]
And this is what I am doing:
final_list = []
for new_list in my_list:
for my_new_list in new_list:
for i in range(2):
final_list.append(my_new_list)
but it shows:
final_list = ['a','a','b','b','c','c','d','d','e','e','f','f']
how do I fix it? and by the way I wish to do it with for loop. Thank you
This will only work for parsing an array of arrays, which I assume should be enough:
final_list = []
for sublist in my_list:
temp_list = []
for item in sublist:
temp_list += [item] * 2
final_list.append(temp_list)
def change(L):
res = []
for i in L:
temp = []
for j in i:
temp.append(j)
temp.append(j)
res.append(list)
return res

Categories