Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Python
  4. hello, good day, does anyone know how to solve this problem? It would help me a lot if you could explain how to do it

hello, good day, does anyone know how to solve this problem? It would help me a lot if you could explain how to do it

Scheduled Pinned Locked Moved Python
helptutorialquestion
3 Posts 3 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15792429
    wrote on last edited by
    #1

    def ListOfLists(list):
    '''
    This function receives a list, which can contain elements that are themselves lists and
    returns those items separately in a single list.
    If the parameter is not of type list, it must return null.
    Receive an argument:
    list: The list that can contain other lists and is converted to a
    list of unique or non-iterable elements.
    Ex:
    ListOfLists([1,2,['a','b'],[10]]) should return [1,2,'a','b',10]
    ListOfLists(108) should return null.
    ListOfLists([[1,2,[3]],[4]]) should return [1,2,3,4]

    L R 2 Replies Last reply
    0
    • M Member_15792429

      def ListOfLists(list):
      '''
      This function receives a list, which can contain elements that are themselves lists and
      returns those items separately in a single list.
      If the parameter is not of type list, it must return null.
      Receive an argument:
      list: The list that can contain other lists and is converted to a
      list of unique or non-iterable elements.
      Ex:
      ListOfLists([1,2,['a','b'],[10]]) should return [1,2,'a','b',10]
      ListOfLists(108) should return null.
      ListOfLists([[1,2,[3]],[4]]) should return [1,2,3,4]

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can use the Built-in Functions type() — Python 3.10.7 documentation[^] builtin function to find out what each item is.

      1 Reply Last reply
      0
      • M Member_15792429

        def ListOfLists(list):
        '''
        This function receives a list, which can contain elements that are themselves lists and
        returns those items separately in a single list.
        If the parameter is not of type list, it must return null.
        Receive an argument:
        list: The list that can contain other lists and is converted to a
        list of unique or non-iterable elements.
        Ex:
        ListOfLists([1,2,['a','b'],[10]]) should return [1,2,'a','b',10]
        ListOfLists(108) should return null.
        ListOfLists([[1,2,[3]],[4]]) should return [1,2,3,4]

        R Offline
        R Offline
        Reverend Jim
        wrote on last edited by
        #3

        A tad late to the party but a recursive approach seems to work.

        def Collapse(lst):

        result = \[\]
        
        for item in lst:
            if type(item) == list:
                result.extend(Collapse(item))
            else:
                result.append(item)
        
        return result
        

        a = [1, [2, 3, [3.1, 3.2]], 4]
        b = Collapse(a)

        print(a)
        print(b)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups