I have two lists in the following format:
list1 = ['A','B','C','D']
list2 = [('A',1),('B',2),('C',3)]
I want to compare the two lists and print out a third list which will have those elements present in list1 but not in list2 and I want to compare only the list2[i][0] elements.
I tried the below code:
fin = [i for i in list1 if i not in list2]
But it prints all the elements in list1. I want the output in the above case to be :
fin = ['D']
Could somebody please suggest how to do that?
Also, I do not want to convert my 2D array list2 to 1D array.
Use the set difference.
set(list1) - set(i[0] for i in list2)
You can do this as well (you need to compare i with the first element of each tuple in list2):
fin = [i for i in list1 if i not in map(lambda(x,_):x,list2)]
How about nested comprehensions:
fin = [a for a in list1 if a not in [b for b,_ in list2]]
Related
I have two lists, for example they look something like this:
list1 = ["Time1Person001", "Time1Person002", "Time1Person003", "Time1Person004", "Time1Person005"]
list2 = ["Time2Person001", "Time2Person003", "Time2Person004", "Time2Person007"]
I want to create a third list that contains only strings that share a substring of the last 3 charachters, so the output should be:
list3 = ["Time1Person001", "Time1Person003", "Time1Person004", "Time2Person001", "Time2Person003", "Time2Person004"]
Efficient way to do it?
Thanks!
Create a set of the common endings then filter each list by that set.
list1 = ["Time1Person001", "Time1Person002", "Time1Person003", "Time1Person004", "Time1Person005"]
list2 = ["Time2Person001", "Time2Person003", "Time2Person004", "Time2Person007"]
endings = set(v[-3:] for v in list1) & set(v[-3:] for v in list2)
list3 = [v for v in list1+list2 if v[-3:] in endings]
Is this what you're after:
list3 = [x for x in list1 for y in list2 if x[-3:] == y[-3:]]
I have 2 list of lists:
list1: [[1,2,3],[2,5],[6,7,4]]
list2: [[1,3],[2],[6,4],[9,0,3]]
I want to do few things:
Find every number that is in list 1 but is not in list 2, store it in a new list, and then append this new list to list2.
In our case: new_list = [5,7]
and then, we will add it to list2 = [[1,3],[2],[6,4],[9,0,3],[5,7]]
Then, I want to remove duplicates of numbers from each list1 and list 2:
In our case: list1 = [[1,3],[2,5],[6,7,4]], list2 = [[1],[2],[6,4],[9,0,3],[5,7]]
I have an implementation using For loops, but I need something more elegant for that.
Can you help me please find out a way?
Using Python, you can do the first part with a set comprehension:
list2.append(list({i for j in list1 for i in j}.difference({i for j in list2 for i in j})))
I have two lists of tuples, say,
list1 = [('item1',),('item2',),('item3',), ('item4',)] # Contains just one item per tuple
list2 = [('item1', 'd',),('item2', 'a',),('item3', 'f',)] # Contains multiple items per tuple
Expected output: 'item4' # Item that doesn't exist in list2
As shown in above example I want to check which item in tuples in list 1 does not exist in first index of tuples in list 2. What is the easiest way to do this without running two for loops?
Assuming your tuple structure is exactly as shown above, this would work:
tuple(set(x[0] for x in list1) - set(x[0] for x in list2))
or per #don't talk just code, better as set comprehensions:
tuple({x[0] for x in list1} - {x[0] for x in list2})
result:
('item4',)
This gives you {'item4'}:
next(zip(*list1)) - dict(list2).keys()
The next(zip(*list1)) gives you the tuple ('item1', 'item2', 'item3', 'item4').
The dict(list2).keys() gives you dict_keys(['item1', 'item2', 'item3']), which happily offers you set operations like that set difference.
Try it online!
This is the only way I can think of doing it, not sure if it helps though. I removed the commas in the items in list1 because I don't see why they are there and it affects the code.
list1 = [('item1'),('item2'),('item3'), ('item4')] # Contains just one item per tuple
list2 = [('item1', 'd',),('item2', 'a',),('item3', 'f',)] # Contains multiple items per tuple
not_in_tuple = []
OutputTuple = [(a) for a, b in list2]
for i in list1:
if i in OutputTuple:
pass
else:
not_in_tuple.append(i)
for i in not_in_tuple:
print(i)
You don't really have a choice but to loop over the two lists. Once efficient way could be to first construct a set of the first elements of list2:
items = {e[0] for e in list2}
list3 = list(filter(lambda x:x[0] not in items, list1))
Output:
>>> list3
[('item4',)]
Try set.difference:
>>> set(next(zip(*list1))).difference(dict(list2))
{'item4'}
>>>
Or even better:
>>> set(list1) ^ {x[:1] for x in list2}
{('item4',)}
>>>
that is a difference operation for sets:
set1 = set(j[0] for j in list1)
set2 = set(j[0] for j in list2)
result = set1.difference(set2)
output:
{'item4'}
for i in list1:
a=i[0]
for j in list2:
b=j[0]
if a==b:
break
else:
print(a)
list1 = ['1_Maths','2_Chemistry','10.1_Geography','12_History']
list2 = ['1_Maths', '2_Physics', '3_Chemistry','11.1_Geography','13_History']
I want to produce two outputs from list1 and list2 based on last 4 characters.
lists = [item for itm in list1 if itm in list2]
The above only prints 1_Maths. Cannot think of a way to produce all the matching subjects.
last_4char = [sub[ : -4] for sub in list1]
This could be an idea but I'm not sure how I can implement this to product the exact results from list1/2
Output
print(new_list1) = ['1_Maths','2_Chemistry','10.1_Geography','12_History']
print(new_list2) = ['1_Maths', '3_Chemistry','11.1_Geography','13_History']
Try this:
def common_4_last(list1, list2):
return [[i for i in list1 if i[-4:] in {k[-4:] for k in list2}], [i for i in list2 if i[-4:] in {k[-4:] for k in list1}]
This will result to a list with 2 elements, one list for items form list1 and a second list for items from list2 that fit the criteria of common last 4
You can run the function for any pair of lists
For example for your given list1 and list2 result will be:
common_4_last(list1, list2)
[['1_Maths', '2_Chemistry', '10.1_Geography', '12_History'], ['1_Maths', '3_Chemistry', '11.1_Geography', '13_History']]
If you want the first list you can get it by
common_4_last(list1, list2)[0] and the same for second list
I’m trying to append a list that’s in another list.
import random
list1 = []
list2 = []
list3 = [list1, list2]
Index = random.choice(list3)
Index = list3.index(Index)
print(Index)
list3[Index].append(“Test”)
print(list3[Index])
I want it to add “test” to either list1 or list2 depending on the value of index.
For some reason, if I repeat the process twice, and the value of index changes, “Test” is added twice to the same list. How do I accomplish this?
random.choice returns one of the items in the given list, which in this case are the references to, rather than the indices of, list1 and list2.
You should append directly to the list returned by random.choice instead:
import random
list1 = []
list2 = []
list3 = [list1, list2]
lst= random.choice(list3)
lst.append('Test')
print(list3)
This can output:
[['Test'], []]