1
[[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [1413], [1658], [1718], [1718], [1035], [1618], [1618]]

this is nested list , every item is a list and i want to make this list as shown bellow:

[1755, 1126, 1098, 1618, 1618,852, 1427, 1044, 852, 1755, 1718, 819, 1323, 1961, 1113, 1126, 1413, 1658, 1718, 1718, 1035, 1618, 1618]

9 Answers9

3

For the most general case this topic already has all the answers.

In this very specific case, you can use the x, = [foo] idiom to unpack an iterable of length one.

>>> lst = [[1755], [1126], [1098], [1618]]
>>> [x for x, in lst]                                                              
[1755, 1126, 1098, 1618]
timgeb
  • 76,762
  • 20
  • 123
  • 145
1

Here is a solution if you have only one element per sub-list:

tmp = []
for sublist in list:
  tmp.append(sublist[0])

Another option:

flat_list = [sublist[0] for sublist in list]

Do not hesitate to upvote + close if this solution suits your needs.

belka
  • 1,480
  • 1
  • 18
  • 31
1
l= [[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [1413], [1658], [1718], [1718], [1035], [1618], [1618]]

flat_list = [item for sublist in l for item in sublist]
flat_list
    [1755, 1126, 1098, 1618, 1618, 852, 1427, 1044, 852, 1755, 1718, 819, 1323,
     1961, 1113, 1126, 1413, 1658, 1718, 1718, 1035, 1618, 1618]
cph_sto
  • 7,189
  • 12
  • 42
  • 78
1

You can use itertools.chain(*iterables):

In [316]: from itertools import chain
In [315]: l = [[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [14
     ...: 13], [1658], [1718], [1718], [1035], [1618], [1618]]

In [317]: list(chain(*l))
Out[317]: 
[1755,
 1126,
 1098,
 1618,
 1618,
 852,
 1427,
 1044,
 852,
 1755,
 1718,
 819,
 1323,
 1961,
 1113,
 1126,
 1413,
 1658,
 1718,
 1718,
 1035,
 1618,
 1618]
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

I guess, you can use extend. This will work with anysize of sublists.

main_list = [[1755], [1126], [1098], [1618]]
resultant_list = []
for subpart in main_list:
    resultant_list.extend(subpart)

enter image description here

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
0

try this:

testlist = [[1755], [1126], [1098]]
ans = [e for e[0] in testlist]
Mohammad Jafari
  • 1,742
  • 13
  • 17
0

Here is another solution using reduce

from functools import reduce

list(reduce(lambda x, y: x + y, lst))

[1755,
 1126,
 1098,
 1618,
 1618,
 852,
 1427,
 1044,
 852,
 1755,
 1718,
 819,
 1323,
 1961,
 1113,
 1126,
 1413,
 1658,
 1718,
 1718,
 1035,
 1618,
 1618]
gold_cy
  • 13,648
  • 3
  • 23
  • 45
0

Maybe you can define a custom method that can also flatten by level:

def flatten(lst, level=1):
  res = []
  for item in lst:
    if isinstance(item, list):
      for subitem in item: res.append(subitem)
    else: res.append(item)
  if level == 1: return res
  else: return flatten(res, level-1)

So you can use it in this way, for example:

lst = [1,[2,[3,[4,[5]]]]]

print(flatten(lst))   #=> [1, 2, [3, [4, [5]]]]
print(flatten(lst,2)) #=> [1, 2, 3, [4, [5]]]
print(flatten(lst,3)) #=> [1, 2, 3, 4, [5]]
print(flatten(lst,4)) #=> [1, 2, 3, 4, 5]

In you case, just:

l = [[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [1413], [1658], [1718], [1718], [1035], [1618], [1618]]
print(flatten(l))
#=> [1755, 1126, 1098, 1618, 1618, 852, 1427, 1044, 852, 1755, 1718, 819, 1323, 1961, 1113, 1126, 1413, 1658, 1718, 1718, 1035, 1618, 1618]
iGian
  • 11,023
  • 3
  • 21
  • 36
0

Here is the solution.

nested_list=[[1755], [1126], [1098], [1618], [1618], [852], [1427], [1044], [852], [1755], [1718], [819], [1323], [1961], [1113], [1126], [1413], [1658], [1718], [1718], [1035], [1618], [1618]]

new_list=[]
for i in nested_list:
    current_element=i[0]
    new_list.append(current_element)
print(new_list)

This is a very beginner friendly solution