-2

I'm not sure what I need to do here. I have a list which contains sublists:

my_list = [
[string_0, string2, [int1, int2], [int3, float1]],
[string_01, string2_2, [int1_1, int2_2], [int3_3, float1_1]]
] 

which goes on like this for a bit.

How do I get certain parts from my_list to create a new list containing certain items from my_list? For example:

new_list = [string_01, string2, int1, float1_1]

So far I was trying to use a list comprehension but I couldn't get it to work because I only got it to print one sublist (ie: string_0, string2, [int1, int2], [int3, float1]) and not specific parts.

JustJoe
  • 19
  • 4
  • Please clarify what you want to achieve. It is unclear what you mean by "certain parts". Without more clarity on how to choose which elements to collect, it is very difficult to help put together code that will solve your problem. – E. Ducateme Oct 11 '17 at 06:39
  • Hope this below reference will workout for you. https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python – Sudhir Oct 11 '17 at 06:40

1 Answers1

0

To get the elements you showed in your example:

new_list=[ [i[0], i[1], i[2][0], i[3][1] ] for i in my_list]
print (new_list)
James
  • 673
  • 6
  • 19
  • Actually, I just noticed this doesn't exactly answer the question, which combines elements from my_list[0] (string2, int1) and my_list[1] (string_01, float1_1). @JustJoe, if you want mix elements form the different sublists, my suggestion won't work. There may be a way to do it with some restrictions, but we would need clarification of your intent. – James Nov 13 '17 at 04:22