I got list like:
[1,1,5,4,6,6,5]
and I want to drop the element of list, if its get repeated.
Output:
[1,5,4,6,5]
I can only find solution for "normal" Duplicate-Problems.
You can use itertools.groupby and just pull the key for each group.
from itertools import groupby
[k for k, _ in groupby([1,1,5,4,6,6,5])]
# returns:
[1, 5, 4, 6, 5]
You can iterate over pairs with zip and build a new list
lst = [1, 1, 5, 4, 6, 5, 5]
lst = [x for x, y in zip(lst, lst[1:]) if x != y] + [lst[-1]]
print(lst) # [1, 5, 4, 6, 5]
Related
Note: I know there is probably an answer for this on StackOverflow already, I just can't find it.
I need to do this:
>>> lst = [1, 2, 3, 4, 5, 6]
>>> first_two = lst.magic_pop(2)
>>> first_two
[1, 2]
>>> lst
[3, 4, 5, 6]
Now magic_pop doesn't exist, I used it just to show an example of what I need. Is there a method like magic_pop that would help me to do everything in a pythonic way?
Do it in two steps. Use a slice to get the first two elements, then remove that slice from the list.
first_list = lst[:2]
del lst[:2]
If you want a one-liner, you can wrap it in a function.
def splice(lst, start = 0, end = None):
if end is None:
end = len(lst)
partial = lst[start:end]
del lst[start:end]
return partial
first_list = splice(lst, end = 2)
One option would be using slices
def func(lst: list, n: int) -> tuple:
return lst[:n], lst[n:]
lst = [1, 2, 3, 4, 5, 6]
first_two, lst = func(lst, 2)
print(lst)
# [3, 4, 5, 6]
Alternative, here is a kafkaesque approach using builtins:
def func(lst: list, i: int) -> list:
return list(map(lambda _: lst.pop(0), range(i)))
lst = list(range(10))
first_two = func(lst, 2)
print(first_two)
# [0, 1]
print(lst)
# [2, 3, 4, 5, 6, 7, 8, 9]
Try:
lst = [1, 2, 3, 4, 5, 6]
first_two, lst = lst[:2], lst[2:]
print(first_two)
print(lst)
Prints:
[1, 2]
[3, 4, 5, 6]
You can just create a lambda function to do this.
magic_pop = lambda x,y:(x[:y],x[y:]) if y<len(lst) else x
magic_pop(lst,3)
Out[8]: ([1, 2, 3], [4, 5, 6])
guys i have to make one list ,by using two distinct list ,and items inside the final list is placed in such a order that ,first element of first list lie just after first element of second list,(lst1=[x1,x2,x3], lst2=[y1,y2,y3] , lst=[x1,y1,x2,y2,x3,y3])
input
lst1=[3,4,7]
lst2=[2,5,1]
ti=0
for yt in range(len(lst1)+1):
if yt %2==0:
lst1.insert(yt,lst2[ti])
ti+=1
print(lst1)
output
[2, 3, 5, 4, 7]
expected
[2, 3, 5, 4, 1, 7]
also wanna know my mistake .
any possible help will be appriciated
Use zip or chain.from_iterable
result = []
for x in zip(lst2, lst1):
result.extend(x)
or
from itertools import chain
list(chain.from_iterable(zip(lst2, lst1)))
[2, 3, 5, 4, 1, 7]
the loop range is wrong in your code:
lst1=[3,4,7]
lst2=[2,5,1]
ti=0
for yt in range(len(lst1)+len(lst2)-1): # <------ here
if yt %2==0:
lst1.insert(yt,lst2[ti])
ti+=1
print(lst1)
You can use zip and itertool's chain
from itertools import chain
li = list(chain(*zip(lst2,lst1)))
Or
li = [j for i in zip(lst2,lst1) for j in i]
li:
[2, 3, 5, 4, 1, 7]
Edit:
Explanation:
3,4,7
0,1,2
first insertion at 0th pos:
2,3,4,7
0,1,2,3
second insertion at 2nd pos:
2,3,5,4,7
0,1,2,3,4
third insertion at 4th pos:
2,3,5,4,1,7
0,1,2,3,4,5
range will be 0,1,2,3,4 [0,5)
expected = [x for tup in zip(lst2,lst1) for x in tup]
Output:
[2, 3, 5, 4, 1, 7]
Try this
lst1 = [3, 4, 7]
lst2 = [2, 5, 1]
lst3 = []
for i in zip(lst2, lst1):
lst3.extend(i)
print(lst3)
Output will this:
[2, 3, 5, 4, 1, 7]
Here I have a list
a = [1, 2, 1, 4, 5, 7, 8, 4, 6]
Now I want a following output but without for loop.
Remove all the duplicate from the list.
[2, 5, 7, 8, 6]
output list contain only single occurrence number
Given: a = [1, 2, 1, 4, 5, 7, 8, 4, 6]
One liner:
b = [x for x in a if a.count(x) == 1]
You can use a Counter and a conditional list comprehension or filter in order to maintain the original order:
from collections import Counter
c = Counter(a)
clean_a = filter(lambda x: c[x] == 1, a) # avoids 'for' ;-)
# clean_a = list(filter(lambda x: c[x] == 1, a)) # Python3, if you need a list
# clean_a = [x for x in a if c[a] == 1] # would be my choice
This is a very simple and inefficient implementation.
We use a while loop to access every element of a. In the loop we check if the current element appears only once in the list. If yes, we add it to a new list.
a = [1, 2, 1, 4, 5, 7, 8, 4, 6]
index = 0
result = []
while index < len(a):
if a.count(a[index]) == 1:
result.append(a[index])
index += 1
print(result)
def cleaner(LIST, pos):
if len(LIST)>pos:
if LIST[pos] in LIST[pos+1:]:
LIST.pop(pos)
# OR
# LIST.remove(LIST[pos])
cleaner(LIST, pos)
else:
pos+=1
cleaner(LIST, pos)
return LIST
LIST = [1, 2, 1, 4, 5, 7, 8, 4, 6]
print(cleaner(LIST, 0))
I have a list:
key: [[1, 3, 4, 5, 3]]
I need to get list like
key: [[6, 5, 8, 7, 6]]
How can I do that with Python?
A simple approach:
>>> my_list = [1, 3, 4, 5, 3]
>>> new_list = []
>>> for i,x in enumerate(my_list):
... if i==0:
... new_list.append(my_list[-1] + my_list[i+1])
... elif i==len(my_list)-1:
... new_list.append(my_list[0] + my_list[i-1])
... else:
... new_list.append(my_list[i-1] + my_list[i+1])
...
>>> new_list
... [6, 5, 8, 7, 6]
you could use list comprehension:
[a[i-1]+(a+a[:1])[i+1] for i in range(len(a))]
this works because:
a[-1] returns the last element of the list
a+a[:1] appends the first element of the list to the end
A functional approach:
from operator import add
list(map(add, key[1:]+key[:1], key[-1:] + key[:-1]))
# [6, 5, 8, 7, 6]
For two lists a and b, how can I get the indices of values that appear in both? For example,
a = [1, 2, 3, 4, 5]
b = [9, 7, 6, 5, 1, 0]
return_indices_of_a(a, b)
would return [0,4], with (a[0],a[4]) = (1,5).
The best way to do this would be to make b a set since you are only checking for membership inside it.
>>> a = [1, 2, 3, 4, 5]
>>> b = set([9, 7, 6, 5, 1, 0])
>>> [i for i, item in enumerate(a) if item in b]
[0, 4]
def return_indices_of_a(a, b):
b_set = set(b)
return [i for i, v in enumerate(a) if v in b_set]
For larger lists this may be of help:
for item in a:
index.append(bisect.bisect(b,item))
idx = np.unique(index).tolist()
Be sure to import numpy.
you can do it like this,with no need to set :
a = [1, 2, 3, 4, 5]
b = {9, 7, 6, 5, 1, 0}
[i for i, item in enumerate(a) if item in b]
inspired by #jamylak