-2

I have a list of lists each of those lists having one element. Is there a "pythonic" way to turn this into a list of elements that aren't lists outside of using the loop displayed below?

un_list = []
for x in home_times:
    y=x[0]
    un_list.append(y)
Pouya Esmaeili
  • 1,265
  • 4
  • 11
  • 25
  • 2
    Does this answer your question? [How do I make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists). Specifically, [this answer](https://stackoverflow.com/a/953097/6340496). – S3DEV Nov 13 '22 at 21:05

2 Answers2

0

You can use list comprehension like this:

sample_input = [[1], [2], [3]]  # list of lists having one element
output = [i[0] for i in sample_input]

And this is the output:

[1, 2, 3]

Read more about list comprehension here.

Pouya Esmaeili
  • 1,265
  • 4
  • 11
  • 25
0

Another way, using sum function.

lst=[['5'], [3],['7'],['B'],[4]]
output= sum(lst, [])
print(output)

Output:

['5', 3, '7', 'B', 4]
AziMez
  • 2,014
  • 1
  • 6
  • 16