1

i have a List a

a = [["v1_0001.jpg","v1_00015.jpg","v1_0002.jpg"],["v2_0001.jpg","v2_0002.jpg","v2_00015.jpg"]]

i want to concatenate these list of list in one list and sort list by alphanumeric

While i am try to sort concatenation list

['v1_0001.jpg', 'v1_00015.jpg', 'v1_0002.jpg','v2_0001.jpg' 'v2_00015.jpg', 'v2_0002.jpg']

I got output like this

['v1_0001.jpg','v1_0002.jpg','v1_00015.jpg','v2_0001.jpg','v2_00015.jpg' 'v2_0002.jpg] 

How to resolve this in python

  • Is something wrong with my answer? – ShlomiF Jun 29 '21 at 05:56
  • You can build a dictionary like that. But that's a separate question and should be posted separately... – ShlomiF Jun 29 '21 at 06:04
  • If this solution is good please "accept" as customary – ShlomiF Jun 29 '21 at 06:05
  • @ShlomiF Hello how i do the same process for alpha numeric like .jpg files –  Jun 29 '21 at 07:55
  • i want to do concatenation and sorting for this a = ["v1_0001.jpg","v1_0002.jpg","v1_0003.jpg","v1_0004.jpg","v1_00015.jpg"] b = ["v2_0001.jpg","v2_0002.jpg","v2_0003.jpg","v2_0004.jpg","v2_00015.jpg"] –  Jun 29 '21 at 08:18
  • You're asking about concatenating? Because string-data works exactly the same. Sorting is a completely different matter and I don't understand what the problem is... – ShlomiF Jun 29 '21 at 10:06
  • @ShlomiF some list i given above comment if is sort that list 00015.jpg comes 2nd elemnt in the list thats why am asking –  Jun 29 '21 at 10:12
  • I answered on your new question. Please accept "correct" answers on both of your posts. – ShlomiF Jun 29 '21 at 10:47

3 Answers3

0

Use chain from itertools, as so -

from itertools import chain

a = [[0, 15, 30, 45, 75, 105],[0, 15, 30, 45, 75, 105]]
b = list(chain(*a))
print(b)
# [0, 15, 30, 45, 75, 105,0, 15, 30, 45, 75, 105]
ShlomiF
  • 2,686
  • 1
  • 14
  • 19
-1

Here is one way. You can do it with recursion

solution :
import itertools
a = [[0, 15, 30, 45, 75, 105],[0, 15, 30, 45, 75, 105]]
print(list(itertools.chain.from_iterable(a)))
  • i think you give a list but my question is list of list Like 'a = [[0, 15, 30, 45, 75, 105],[0, 15, 30, 45, 75, 105]]' –  Jun 29 '21 at 05:46
  • @HarryMc, I give a list of list as an argument –  Jun 29 '21 at 05:47
-1

Maybe this is not the best to use in big lists but for understanding the logic might be good:

>>> a = [[0, 15, 30, 45, 75, 105],[0, 15, 30, 45, 75, 105]]
>>> b = []  # Creates an empty list
>>> for list_inside_a in a:  # For every list in a
...     b+= list_inside_a  # Concatenates n-th list with b
...
>>> b
[0, 15, 30, 45, 75, 105, 0, 15, 30, 45, 75, 105]
Merrydjff
  • 166
  • 2
  • 7