7

I am working with a list of lists longitudes and latitudes. Each inner list has an extra pair or square brackets.

For instance:

[[-87.77621462941525,-87.77676645562643,-87.77906119123564]]

I would like to remove the extra brackets and be left with:

 [-87.77621462941525,-87.77676645562643,-87.77906119123564] 

Thanks

AlSub
  • 1,384
  • 1
  • 14
  • 33
mwaks
  • 379
  • 2
  • 4
  • 10

2 Answers2

11

this is nested list object, you can access the inner list with index.

In [1]: l = [[-87.77621462941525,-87.77676645562643,-87.77906119123564,]]

In [2]: l[0]
Out[2]: [-87.77621462941525, -87.77676645562643, -87.77906119123564]
宏杰李
  • 11,820
  • 2
  • 28
  • 35
4

List Indexing

You can simply use list indexing.

my_lists = [[-87.77621462941525,-87.77676645562643,-87.77906119123564,]]
my_list  = my_lists[0]

next

Another way of doing this is using next which takes an iterable as an argument. It will raise a StopIteration in the case of the iterable being empty. You can prevent this by adding a default value as a second argument.

my_list = next(iter(my_lists))

Or

my_list = next(iter(my_list), [])  # Using [] here but the second argument can be anything, really

Obviously this second option might be a bit of overkill for your case but I find it a useful one-liner in many cases. For example:

next_element = next(iter(list_of_unknown_size), None)

Versus

next_element = list_of_unknown_size[0] if list_of_unknown_size else None
aydow
  • 3,673
  • 2
  • 23
  • 40