Finding only pairs in list using list comprehension

Related searches

Looking for a fancy one line solution to finding pairs of items in a list using list comprehension.

I've got some code that finds multiples, but can't figure out how to split those multiples into pairs.

lst = [1,2,4,2,2,3,3,1,1,1,2,4,3,4,1]
len(set([x for x in lst if lst.count(x) > 1]))

Code above returns 4. Answer should be 6 pairs, [1,1,1,1,1] = 2, [2,2,2,2] = 2, [3,3,3] = 1 and [4,4,4] = 1.

A one-liner with no other intermediate variables would be:

sum(lst.count(x)//2 for x in set(lst))

It loops over set(lst) which contains all the distinct numbers in lst, and adds their pair counts.

Python, Python List Comprehension to find pair with given sum from two arrays � Python - Numeric Sort in Mixed Pair String List � Backward iteration in� We can solve this problem quickly in python using List comprehension. Approach is very simple, we will consider all those pairs for which if k lies in arr2 then x-k should lie in arr1 , so pair will be (x-k,k).

Another approach would be using [Python 3.Docs]: class collections.Counter([iterable-or-mapping]):

>>> from collections import Counter
>>>
>>> lst = [1, 2, 4, 2, 2, 3, 3, 1, 1, 1, 2, 4, 3, 4, 1]
>>>
>>> c = Counter(lst)
>>> c
Counter({1: 5, 2: 4, 4: 3, 3: 3})
>>>
>>> sum(item // 2 for item in c.values())
6

and the one line equivalent:

>>> sum(item // 2 for item in Counter(lst).values())
6

Python, Sometimes, while working with lists, we need to pair up the like elements The list comprehension can be easily used to perform this particular task, but This task can also be achieved using only the zip function which performs Please Improve this article if you find anything incorrect by clicking on the� Method #1 : Using list comprehension List comprehension can be used to print the pairs by accessing current and next element in the list and then printing the same. Care has to be taken while pairing the last element with the first one to form a cyclic pair.

You can do the following (if I've understood your pairing method properly):

lst = [1,2,4,2,2,3,3,1,1,1,2,4,3,4,1]
the_dict = {x: int((lst.count(x)/2)) for x in lst}

print(sum(the_dict.values()))

> 6

print(the_dict)

> {1: 2, 2: 2, 4: 1, 3: 1}

This makes a dictionary with the count of all pairs, then you can sum the values in the dictionary to get the pair count. Then you also have the dictionary available with the pair count of each value, if you need it.

(Tutorial) Python List Comprehension, Learn how to effectively use list comprehension in Python to create lists, When doing data science, you might find yourself wanting to read lists of lists, filtering column If you only want to extract expression for a specific set of data from two pairs of square brackets to change the logic of your nested list� Sometimes, while working with lists, we need to pair up the like elements in the list and then store them as lists of lists. This particular task has its utility in many domains, be it web development or day-day programming. Let’s discuss certain ways in which this can be achieved. Method #1 : Using list comprehension

When to Use a List Comprehension in Python – Real Python, Python list comprehensions make it easy to create lists while rewrite the for loop from the first example in just a single line of code: To create the squares dictionary, you use curly braces ( {} ) as well as a key-value pair ( i: i * i ) in your you can find more information from Python Naming and Binding. Let’s discuss certain ways to print all the alternate elements of the given list. Method #1 : Using list comprehension. Shorthand to the naive method, list comprehension provides a faster way to perform this particular task. In this method, all the indices which are not multiple of 2, hence odd are inserted in the new list.

5. Data Structures — Python 3.8.5 documentation, Extend the list by appending all the items from the iterable. that methods like insert , remove or sort that only modify the list have no return value List comprehensions provide a concise way to create lists. We'll find other uses for del later. Empty tuples are constructed by an empty pair of parentheses; a tuple with one� Finding squares using list comprehensions: Here, square brackets signify that the output is a list . n**2 is the expression executed for each element and for n in numbers is used to iterate over

Using list comprehension in functions. Now, let’s see how we can use list comprehension in functions. # Create a function and name it double: def double(x): return x*2 # If you now just print that function with a value in it, it should look like this: >>> print double(10) 20 We can easily use list comprehension on that function.

Comments
  • Why for 1 and 2 there are 2 pairs while for 3 and 4 only 1?
  • Because [3 3 3] is one pair and a single value. [2,2,2,2] is an even double pair.
  • So, only looking for pairs of items in each sub-list. For example. [1,1] = 1, [1,1,1] = 1, [1,1,1,1] = 2.
  • Yeah. That was my original thought, but I wanted to avoid importing counter. It's another clean solution though.
  • The only sane answer here (O(n))
  • This is great solution as well. I like how the dictionary can also give you the pairs.