For example if:
List = [1,2,3,4,5]
I want like this:
List = [1,1,2,2,3,3,4,4,5,5]
For example if:
List = [1,2,3,4,5]
I want like this:
List = [1,1,2,2,3,3,4,4,5,5]
Try this:
li = [1,2,3,4,5]
print(sorted(li+li))
you can use zip with chain(for flatten):
from itetools import chain
List = list(chain(*zip(List,List)))
print(List)
output:
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
or you can use a for
loop:
new_list = []
for n in List:
new_list.extend([n, n])
List = new_list
print(new_list)
output:
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
Maybe this, if there is no notion of sorting:
myList = [1,2,3,4,5]
for idx in range(0, len(myList)*2, 2):
myList.insert(idx+1, myList[idx])
print(myList)
You can use extend
. If you want to preserve the order.
l = [2,1,3,5,6]
res = []
for i in l:
res.extend((i, i))
print(res)
output
[2, 2, 1, 1, 3, 3, 5, 5, 6, 6]
res = []
l = [None, True, 5, 1, 8]
for i in l:
res.extend((i, i))
print(res)
output
[None, None, True, True, 5, 5, 1, 1, 8, 8]