-2

There is a list contains sub-list like follows:

country_list = ['pakistan', 'india', 'iran', 'china', 'afghanistan', ['pakistan', 'iran', 'india']]

Output should be: ['pakistan', 'india', 'iran']

There can be more than one sub-lists in main list like:

countries = ['pakistan', 'india', 'iran', 'china', 'afghanistan', ['pakistan', 'india'], ['china', 'pakistan']]

Output should be: ['pakistan', 'india', 'china']

I need a generic function which returns a list containing duplicates.

Azeem
  • 292
  • 2
  • 13
  • Cool stuff dude. Get coding. Show us your results. SO is about fixing _your_ Code - not implementing your ideas. Please go over [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic) again and if you have questions provide your code as [mvce](https://stackoverflow.com/help/mcve). If you encounter errors, copy and paste the error message verbatim ( word for word) into your question. Avoid using screenshots unless you need to convey layout errors. We can NOT copy and paste your image into our IDEs to fix your code. – Patrick Artner Apr 27 '18 at 20:11
  • Google "python flatten list", that will help deal with nested lists. – Alex Hall Apr 27 '18 at 20:21
  • This should help: https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists. Once you've flattened it, run it through Counter. – W Stokvis Apr 27 '18 at 20:22
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on his own. A good way to show this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/help/how-to-ask). – Rory Daulton Apr 27 '18 at 20:22
  • @RoryDaulton, I'm sorry if it hurts you, I had tried it, but due to lack of knowledge I wasn't able to create a logic for this problem. I'm beginner in python. – Azeem Apr 27 '18 at 20:55
  • @AlexHall Thank you for enlighten me with "python flatten list". Now I'm able to solve this problem. – Azeem Apr 27 '18 at 20:58
  • @WStokvis Thank you, your suggestion was quite helpful understanding the problem. – Azeem Apr 27 '18 at 20:58

3 Answers3

1

It might help if you phrased your question a bit more clearly. But if your lists aren't too big, you might want to use sets:

countries = frozenset(x for x in country_list if isinstance(x, str))
subsets = frozenset.union(*(frozenset(x) for x in country_list if not isinstance(x, str)))
output = list(countries.intersection(subsets))

Do note that it might be the case that a for-loop to build countries and subsets might be more efficient. This depends on the potential speed-up of constructors like this versus the costs of using isinstance twice. The output is also not sorted.

Martijn
  • 417
  • 2
  • 8
0

Ugly but works - does not preserve the order because set is an unordered datatype:

lst1 = list(set([j for x in [i for i in counties if isinstance(i, list)] for j in x]))
print(lst1)

Returns:

['pakistan', 'iran', 'india']
What
  • 304
  • 1
  • 12
  • `type(subarray) == type([])` is really ugly. Why not `isinstance(subarray, list)`? – Patrick Haugh Apr 27 '18 at 20:28
  • This doesn't resolve the second case though: the return value won't include `China`. – Martijn Apr 27 '18 at 20:31
  • It might also be a better idea to use a `for`-loop or a generator expression in combination with `next` here. Now you need to build the entire list just to get the first match. – Martijn Apr 27 '18 at 20:32
  • @Martijn should be fixed I only selected the first element. – What Apr 27 '18 at 20:33
  • @What that doesn't resolve the issue: it'll return `[['pakistan' 'india'], ['china', 'pakistan']]` instead of `['pakistan', 'india', 'china']` and also excludes any potential non-matches. – Martijn Apr 27 '18 at 20:34
0

As @Alex referred to learn about "Flatten list", I came up with the solution:

Suppose there is a list of lists or a list containing sub-lists:

import collections
countries = ['pakistan', 'india', 'iran', 'china', 'afghanistan', ['pakistan', 'india'], ['china', 'pakistan']]

def flatten_list(c_list):
"""
function to convert list of lists into flat list.
"""
  result = []
  for country in c_list:
      if isinstance(country, collections.Iterable) and not isinstance(country, (str, bytes)):
          result.extend(flatten_list(country))
      else:
          result.append(country)
  return result

def list_of_duplicates(flat_list):
"""
function to get duplicate entries and removing single entries
"""

    new_list = flat_list
    for e in set(new_list):
        new_list.remove(e)
    return list(set(new_list))


  result = list_of_duplicates(flatten_list(countries)) #desired result
Azeem
  • 292
  • 2
  • 13