-1

For example if:

List = [1,2,3,4,5]

I want like this:

List = [1,1,2,2,3,3,4,4,5,5]
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
venki
  • 39
  • 1
  • 4
  • 1
    What if `List = [3, 1, 4, "allo", 42, False, None]` ? – bruno desthuilliers Jan 08 '20 at 09:03
  • [duplicate](https://stackoverflow.com/q/2449077/1324033) - Put effort into your question(s) before posting. – Sayse Jan 08 '20 at 09:09
  • @Sayse it's sometimes difficult to find duplicates (look at all the answers below). Just vote to close (I followed) – Jean-François Fabre Jan 08 '20 at 09:16
  • @Jean-FrançoisFabre - Thats true, but when I could find a duplicate with a [single google search for "python duplicate list"](https://imgur.com/a/2zYw2Ob), then I find it hard to believe the op put any effort in at all – Sayse Jan 08 '20 at 09:18
  • Sure, Thanks for our suggestion. – venki Jan 08 '20 at 09:18
  • the added value of high rep users like you is to help newcomers to find the duplicates. Let's imagine one second that the OP thinks that stackoverflow search works properly for instance :) – Jean-François Fabre Jan 08 '20 at 09:20
  • @Jean-FrançoisFabre - I'll be doing a lot more helping when I get the dupe-hammer :) In the mean time, OP your posts like this are going to continue to get downvoted so I'd suggest reading [ask] – Sayse Jan 08 '20 at 09:27
  • 1
    @Sayse I remember that the last 130 points are the toughest to get. Good luck :) – Jean-François Fabre Jan 08 '20 at 10:15

5 Answers5

3

Try this:

li = [1,2,3,4,5]

print(sorted(li+li))
Shweta Chandel
  • 887
  • 7
  • 17
  • 3
    This only provides the required output for this specific example. It will provide wrong output for any non-sorted input. Try `sorted(li + li, key=li.index`) – DeepSpace Jan 08 '20 at 09:04
  • 2
    @DeepSpace that would really kill the efficiency, better to create a index map, so `sorted(li + li, key={x:i for i,x in enumerate(li)}.get)` – juanpa.arrivillaga Jan 08 '20 at 09:07
  • @juanpa.arrivillaga or just `out = [] ; for i in li: li.append(i) ; li.append(i)` simple O(n) with no sorting (O(nlogn)) – DeepSpace Jan 08 '20 at 09:10
  • @DeepSpace sure. Essentially, Jean-François Fabre's answer. But using a `key=li.index` will make it O(N**2 logN) – juanpa.arrivillaga Jan 08 '20 at 09:11
  • Thank you, **sorted(li + li, key=li.index)** this solution worked fine. – venki Jan 08 '20 at 09:12
  • 1
    @venki don't use that key function, it kills the efficiency. n * logn is much better than quadratic time. use an index map. – juanpa.arrivillaga Jan 08 '20 at 09:13
  • @juanpa.arrivillaga Why O(N\*\*2 logN)? Pretty sure it's just O(N\*\*2). – Stefan Pochmann Jan 08 '20 at 10:30
  • @juanpa.arrivillaga I don't understand what you mean, but I suspect you think the key function is called *during* the sorting for each comparison instead of just once for each element *before* the sorting? – Stefan Pochmann Jan 08 '20 at 10:38
  • @StefanPochmann ahhh right, actually, it isn't, it is only called once for each, so something like `N**2 + N* log N` => O(N**2) is what you mean? Yes I believe you are correct. – juanpa.arrivillaga Jan 08 '20 at 10:41
  • 1
    @juanpa.arrivillaga Yes, that's what I think. – Stefan Pochmann Jan 08 '20 at 10:41
  • @juanpa.arrivillaga Lol... this actually gave me a really bad idea, and it does work: `sorted(a + a, key=lambda _, k=iter(list(range(len(a))) * 2): next(k))`. – Stefan Pochmann Jan 08 '20 at 10:55
  • @StefanPochmann that can actually be efficient, no need to materialize the list! just `sorted(a + a, key=iter(range(len(a)*2)).__next__)`, – juanpa.arrivillaga Jan 08 '20 at 11:01
  • Please improve my answer or post a new one after you conclude your best answer. – Shweta Chandel Jan 08 '20 at 11:03
  • @juanpa.arrivillaga Nah, that doesn't work. For two big reasons. – Stefan Pochmann Jan 08 '20 at 11:04
  • @juanpa.arrivillaga Well, your second problem is that you create keys from 0 to 2n-1, which won't work for the sorting. But this works: `sorted(a + a, key=lambda _, k=itertools.cycle(range(len(a))): next(k))`. And [this](https://stackoverflow.com/a/59644433/1672429), too. – Stefan Pochmann Jan 08 '20 at 11:14
  • @StefanPochmann yes, damn, I see now. I actually prefer `sorted(a + a, key=lambda _, k=itertools.chain(*[range(len(a))]*2): next(k))` – juanpa.arrivillaga Jan 08 '20 at 11:14
  • @juanpa.arrivillaga You prefer it over my original or over my `cycle` version? (not sure you saw the latter, as I ninja'd you) – Stefan Pochmann Jan 08 '20 at 11:17
  • @StefanPochmann over both, because it requires the least amount of space, `cycle` materializes a list underneath the hood, I'm pretty sure – juanpa.arrivillaga Jan 08 '20 at 11:18
  • @juanpa.arrivillaga Oh, right. It *could* recognize that it's a `range` and not materialize, but I doubt it does. – Stefan Pochmann Jan 08 '20 at 11:21
  • @juanpa.arrivillaga I think not just `range` but all immutable sequences would allow that optimization, and they [are](https://docs.python.org/3/library/stdtypes.html#immutable-sequence-types) a thing, but I had a look at [the code](https://github.com/python/cpython/blob/master/Modules/itertoolsmodule.c#L942) and don't see any such optimization. It seems to indeed always store the elements in an internal list during the first iteration. – Stefan Pochmann Jan 08 '20 at 11:38
2

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]
kederrac
  • 16,819
  • 6
  • 32
  • 55
1

You can zip the list with itself:

[i for t in zip(List, List) for i in t]
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

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)
Clément
  • 1,128
  • 7
  • 21
0

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]
kederrac
  • 16,819
  • 6
  • 32
  • 55
Ch3steR
  • 20,090
  • 4
  • 28
  • 58