Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in flattening list
#1
I tried to flatten list.I have nested list and i want all iterables of that nested list in one list without any nested list. Itried with itertool.chain and also with recursive function.

import itertools

a = [[1,2,3], [4,7,8],9]
b = list(itertools.chain(a))
print(b)
def flatten(list):
	a=[]
	for i in list:
		if type(i) == list:
			flatten(i)
		else:
			a.append(i)
	return a
	
b =  [[1,2,3], [4,7,8],9]
c = flatten(b)
print(c)
I still get nested list in result. No changes
Reply


Messages In This Thread
Problem in flattening list - by Shahmadhur13 - May-02-2020, 03:52 PM
RE: Problem in flattening list - by pyzyx3qwerty - May-02-2020, 03:57 PM
RE: Problem in flattening list - by bowlofred - May-02-2020, 04:03 PM
RE: Problem in flattening list - by DeaD_EyE - May-02-2020, 10:31 PM
RE: Problem in flattening list - by bowlofred - May-02-2020, 11:40 PM
RE: Problem in flattening list - by DeaD_EyE - May-03-2020, 12:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with "Number List" problem on HackerRank Pnerd 5 2,086 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  Flattening attribute access ruy 5 2,064 Jun-25-2021, 08:26 PM
Last Post: ruy
  flattening a list with some elements being lists Skaperen 17 7,444 Apr-09-2019, 07:08 AM
Last Post: perfringo
  Flattening List mp3909 8 5,225 Jan-26-2018, 12:13 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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