Python Forum
Making list empty after return in function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making list empty after return in function
#6
If you have only a nested list, not a deep nested list, you can use itertools.chain.from_iterable


from itertools import chain


flatten = chain.from_iterable
nested = [('a', 'b', 'r', 'a'),('c', 'a', 'd', 'a', 'b', 'r', 'a')]
result = list(flatten(nested))

print(result)
Output:
['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a']
chain.from_iterable returns an iterator. You have to consume this iterator to result by result.
If you always want to have a list as result, you can make a function for it.

from itertools import chain


def flatten_to_list(iterable):
    return list(chain.from_iterable(iterable))


nested = [('a', 'b', 'r', 'a'),('c', 'a', 'd', 'a', 'b', 'r', 'a')]

print(flatten_to_list(nested))
Output:
['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a']
Reference from Python documentation: itertools itertools-recipes

Do you like it? You should read the whole description of this module. It's very powerful. Mostly all parts of Python language have implemented iteration. The benefit is also, that you can work with "big data" or data-streams. The iterator protocol allows this. The function flatten_to_list removes this ability, because the list is constructed in memory until the iteration ends. It's not possible with infinite streams, except if you use itertools.islice(iterable, start, end).

In addition, you get back, what you want. If you want to have a set, then use set(chain.from_iterable(nested_something)).
This can be done also with tuple, list, dict.fromkeys and other types which consumes iterables.


Bonus: You can do it also with str.join

result1 = str.join('-', flatten(nested))
# or
result2 = '-'.join(flatten(nested))

print(result1)
print(result2)
Output:
a-b-r-a-c-a-d-a-b-r-a a-b-r-a-c-a-d-a-b-r-a
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Making list empty after return in function - by DeaD_EyE - Nov-24-2018, 09:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  what to return for an empty list Skaperen 2 299 May-24-2024, 05:17 PM
Last Post: Skaperen
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 16,145 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Code with empty list not executing adeana 9 3,960 Dec-11-2023, 08:27 AM
Last Post: buran
  nested function return MHGhonaim 2 754 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,671 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,332 Feb-04-2023, 12:30 PM
Last Post: caslor
  Making a function more efficient CatorCanulis 9 2,088 Oct-06-2022, 07:47 AM
Last Post: DPaul
  set.difference of two list gives empty result wardancer84 4 1,645 Jun-14-2022, 01:36 PM
Last Post: wardancer84
  Showing an empty chart, then input data via function kgall89 0 1,047 Jun-02-2022, 01:53 AM
Last Post: kgall89
  displaying empty list vlearner 5 1,808 Jan-19-2022, 09:12 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020