This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(31 answers)
Closed 9 years ago.
I have two lists. How can I define a function "remove_repeat_element" to remove repeat elements from them ? ?
def remove_repeat_element(a, b):
... ...
a = ['bd09fdf7-918e-4a5e-8338-0f6fe78fd238']
b = ['bd09fdf7-918e-4a5e-8338-0f6fe78fd238', '3c26f383-da50-446c-8613-64e1068bd57e']
result = remove_repeat_element(a, b)
print result
>>> ['3c26f383-da50-446c-8613-64e1068bd57e']
Could someone give me some advice ??
Thanks a lot!
You can use sets :
>>> a = ['bd09fdf7-918e-4a5e-8338-0f6fe78fd238']
>>> b = ['bd09fdf7-918e-4a5e-8338-0f6fe78fd238', '3c26f383-da50-446c-8613-64e1068bd57e']
>>> list(set(b) - set(a))
['3c26f383-da50-446c-8613-64e1068bd57e']
My suggestion:
c = [x for x in b if x not in a]
It is pretty straight forward to write and efficient enough as a list comprehension.
Hope this helps!
Update:
For more efficient membership checking, use set instead of list.
a = set(a)
c = [x for x in b if x not in a]
This would be even faster and not to mention the improvement when the list is large.
return [el for el in b if el not in a]
Related
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 3 years ago.
I'm setting up an algorithm and I do not achieve to create a list for each loop index.
I don't know how can I change the names of lists during the for-loop.
for i in range(10):
list = list.append(1 + i)
In order to get:
list0 = [1]
list1 = [2]
list2 = [3]
and so on.
My objective is to call these lists on another function, is there a way to name these lists with the list index?
I don't have find any subject which describe my issue. Maybe I don't clearly explain my problem.
It isn't efficient to create those variables, instead a dictionary would come in handy:
d = {}
for i in range(10):
d['list%s' % i] = [i + 1]
It actually could be done with:
d = {'list%s' % i: [i + 1] for i in range(10)}
To answer your question, you can do this but I think that it is not a best practice for your issue:
for i in range(10):
locals()['list{}'.format(i)] = [1 + i]
You can see that this code created 10 variables :
>>>print(list0)
[1]
>>>print(list9)
[10]
I'd prefer using a dict as in #U9-Forward answer
Instead of doing that assign them all to a reference in another list e.g.
Lst [0] = [1]
Lst [1] = [2]
This is done with:
Lst=[]
for i in range(10):
Lst.append(i+1)
This question already has answers here:
What is the most efficient way to search nested lists in python?
(5 answers)
Search in lists of lists by given index
(13 answers)
Closed 4 years ago.
I am given:
list = [{'a','b'},{'c','d'}]
and I want to know if 'a' is in the list.
should I first unpack the list as here to new_list and the use 'a' in new_list
Or is there a shorter way (without importing modules)
use any
spam = [{'a','b'},{'c','d'}]
eggs = [{'d','b'},{'c','d'}]
print(any('a' in my_set for my_set in spam))
print(any('a' in my_set for my_set in eggs))
Output
True
False
>>>
This is one approach using any.
Ex:
l = [{'a','b'},{'c','d'}]
print( any(map(lambda x: "a" in x, l)) )
Output:
True
Hope it's solve it. I write it on a function to use "return" keyword
def a_is_there():
lists = [{'a','b'},{'c','d'}]
for list in lists:
if "a" in list:
print("True")
return True
print("False")
return False
a_is_there()
Thanks
This question already has answers here:
Most pythonic way to interleave two strings
(14 answers)
Closed 4 years ago.
How to join two or more strings in python with one character after another?
For example
a = 'hello'
b = 'world'
output = 'hweolellod'
Same goes for three or more strings. using + is not helping.
You could try this:
''.join([x + y for x, y in zip(a, b)])
which gives:
'hweolrllod'
One way is to use str.join with itertools:
from itertools import chain, zip_longest
a = 'hello'
b = 'world'
zipper = zip_longest(a, b, fillvalue='')
res = ''.join(list(chain.from_iterable(zipper)))
print(res)
hweolrllod
Explanation
zip_longest is used to account for inconsistent length strings.
zipper here is a lazy iterator which iterates each character of a and b simultaneously by index.
List creation, while not necessary, is more efficient with str.join.
This question already has answers here:
Ellipsis lists [...] and concatenating a list to itself [duplicate]
(3 answers)
Closed 6 years ago.
I have a list X = ['xyz']
I use the below commands for appending to another variable.
L = X
L.append(X)
L
Out[109]: ['xyz', [...]]
I am unable to understand why the second element in the new L list is not having the value as 'xyz'
My question is not how to append or extend a list, but in fact about the functionality of Append function, which has been correctly explained by #sinsuren below.
append add X as an element to L. If you want the element inside X to be inserted to L, use extend instead:
>>> X = ['xyz']
>>> L = X
>>> L.extend(X)
>>> L
['xyz', 'xyz']
Try this, it will extend the list.
L.extend(X)
But if you want to use append. Use a element like this L.append('abc'). It will give same result or L.append(X[0])
Edit: You have Appended list to itself. It will recursively append to itself and due to which even L[1] will give you same response. Like L[1] = ['xyz', [...]] . and for more understanding Please refer What's exactly happening in infinite nested lists?
This question already has answers here:
Removing duplicates from a list of lists
(16 answers)
Closed 4 years ago.
I want to remove all duplicates list from a list of list.
So I have a list of lists like this.
a = [[1,2],[1,2],[3,4,5],[3,4,5],[3,4,5]]
I want to have:
b = [[1,2],[3,4,5]]
I don't know how to do it.
You could use a set:
b_set = set(map(tuple,a)) #need to convert the inner lists to tuples so they are hashable
b = map(list,b_set) #Now convert tuples back into lists (maybe unnecessary?)
Or, if you prefer list comprehensions/generators:
b_set = set(tuple(x) for x in a)
b = [ list(x) for x in b_set ]
Finally, if order is important, you can always sort b:
b.sort(key = lambda x: a.index(x) )
See mgilson's answer if the order of the lists is not important. If you want to retain the order, do something like:
b = list()
for sublist in a:
if sublist not in b:
b.append(sublist)
This will keep the order in the original list. However, it is slower and more verbose than using sets.