get half of all repeating elements in a list

Question:

My goal is to create a list with half of the individual items repeating. In the list below I have given an example. In this list, except for the first element, each marked element is repeated 2n times, therefore all elements are even and divisible by 2. I can’t create a list comprehension that divides by 2 the number of times each element is repeated. For example the element: [0, 255, 0] is repeated 6 times in the list; I expect in my new list to be repeated only 3 times.

Input list:

[[255, 255, 255], [255, 0, 0], [255, 0, 0], [0, 255, 0], [0, 255, 0],
 [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

Expected output list:

[[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]
Asked By: iPc

||

Answers:

  res_lst = [item for k, item in enumerate(list) if k == 0 or k % 2 != 0]

    print(res_lst)

    res_lst = [[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]
Answered By: shubham koli

You can use answer from shubham koli than create a new list as follow

list = [
  [255, 255, 255],
  [255, 0, 0], [255, 0, 0],
  [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]
]


temp_list = []
for i in list:
    if i not in temp_list:
        temp_list.append(i)

result_list = []
for i in range(0, len(temp_list)):
  if i == 0:
    result_list.append(temp_list[i])
  else:
    n = list.count(temp_list[i])
    for j in range(n // 2):
      result_list.append(temp_list[i])

print(result_list)

which produces

[[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

as requested. Number of repreated items is store in n and is obtained using .count() from a list. Later n // 2 will give half of its value in integer.

Answered By: dudung

You could use the Counter class form collections to compute the number of repetition then a comprehension to repeat the distinct values half the number of times they occurred:

from collections import Counter

L = [[255, 255, 255], [255, 0, 0], [255, 0, 0], [0, 255, 0], 
    [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

R = [ t for t,count in Counter(map(tuple,L)).items() 
        for _ in range(count//2 or 1) ]

print(R)    
[(255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)]

Note that i’m converting the sublists into tuple because the Counter dictionary needs a hashable key value. You can convert them back into lists in the comprehension if you need the output to be a list of lists.

For consecutive repetitions:

If your repetitions are always consecutive, you can use a more efficient approach by filtering/including every other repetition (at even relative occurence):

Using a simple loop:

include  = True
result   = L[:1]
for s in L[1:]:
    include = True if s != result[-1] else not include
    if include:
        result.append(s)

print(result)
[[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]

or the groupby/islice functions from itertools:

from itertools import groupby,islice

result = [ s for _,g in groupby(L) for s in islice(g,0,None,2) ]

print(result)
[[255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]    
 
Answered By: Alain T.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.