Expand list with identical elements

Related searches

Suppose I have the following nested list:

initial_list = [[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]]

I would like to turn it into the following:

desired_list = [[1, 1, 1, 2, 2, 2, 3, 3, 3], 
                [4, 4, 4, 5, 5, 5, 6, 6, 6], 
                [7, 7, 7, 8, 8, 8, 9, 9, 9]]

If I didn't care about order, I could do something like

new_list = [sorted(x*3) for x in initial_list]

However, the order should remain the same as in the initial_list. The best I could make is to put each element in a list and multiply it by 3 (arbitrary number), and then join the resulting inner_list:

multiplied_list = [[[element]*3 for element in inner_list] for inner_list in initial_list]
desired_list = [[element for element_list in inner_list for element in element_list] for inner_list in multiplied_list]

(In two lists for human comprehension)

Is there a more comprehensible/adequate/pythonic way to perform this?

You could just use the following list-comprehension. Note my initial_list is different to the one in the OP to demonstrate that order is preserved.

Code:

>>> initial_list = [[1, 3, 2], [4, 5, 6], [7, 8, 9]]
>>> [[x for x in sl for _ in range(3)] for sl in initial_list]
[[1, 1, 1, 3, 3, 3, 2, 2, 2],
 [4, 4, 4, 5, 5, 5, 6, 6, 6],
 [7, 7, 7, 8, 8, 8, 9, 9, 9]]

Or, add a key to the sorted function in your example:

>>> [sorted(x*3, key=x.index) for x in initial_list]
[[1, 1, 1, 3, 3, 3, 2, 2, 2],
 [4, 4, 4, 5, 5, 5, 6, 6, 6],
 [7, 7, 7, 8, 8, 8, 9, 9, 9]]

Time comparison of methods with varying n*n list size:

Generated using perfplot - code to reproduce:

from itertools import chain
from functools import reduce
import perfplot
from copy import deepcopy
import numpy as np
import random

def shuffle(x):
    random.shuffle(x)
    return x

def cdjb(initial_list):
    return [[x for x in sl for _ in range(3)] for sl in initial_list]

def aurora_sorted(initial_list):
    return [sorted(x*3, key=x.index) for x in initial_list]

def aurora_list_comp(initial_list):
    return [[element for element_list in inner_list for element in element_list] for inner_list in [[[element]*3 for element in inner_list] for inner_list in initial_list]]

def kederrac(initial_list):
    new_list = deepcopy(initial_list)
    for l in new_list:
        for j in range(0, 3*len(l), 3):
            l[j: j + 1] = [l[j]] * 3
    return new_list

def alain_chain(initial_list):
    return [list(chain(*(i3 for i3 in zip(*[sl]*3)))) for sl in initial_list]

def alain_reduce(initial_list):
    return [list(reduce(lambda a,e:a+[e]*3,sl,[]))for sl in initial_list]

def alain_zip(initial_list):
    return [[i for i3 in zip(*[sl]*3) for i in i3] for sl in initial_list]

def binyamin_numpy(initial_list):
    return np.array(initial_list).repeat(3).reshape(len(initial_list), -1).tolist()


perfplot.show(
    setup=lambda n: [shuffle([i for i in range(n)]) for j in range(n)],
    n_range=[2**k for k in range(12)],
    kernels=[
        cdjb,aurora_sorted, aurora_list_comp, kederrac, alain_chain, alain_reduce, alain_zip, binyamin_numpy
        ],
    xlabel='len(x)',
    )

Repeating elements of a list n times, Related questions: Circular list iterator in Python, Duplicate elements in a list, Repeat a list within a list X number of times, Best way to extend a list with itself N � At first glance it seems that repeat is the fastest way to create a list with n identical elements: >>> timeit.timeit('itertools.repeat (0, 10)', 'import itertools', number = 1000000) 0.37095273281943264 >>> timeit.timeit(' * 10', 'import itertools', number = 1000000) 0.5577236771712819 But wait - it's not a fair test

numpy it, and 1 line of code:

 arr=np.array(initial_list)
 arr.repeat(3).reshape(3,-1)

output:

Out[44]: 
array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
       [4, 4, 4, 5, 5, 5, 6, 6, 6],
       [7, 7, 7, 8, 8, 8, 9, 9, 9]])

Python, Adding a single value in list is quite generic and easy. part is responsible for performing the task of repetition of elements desired number of times. using extend() + list comprehension to add multiple values n times in given list � Python | Remove elements of list that are repeated less than k times� The original list is : [1, 4, 5, 5, 5, 9, 1] The list of duplicate elements is : [3, 4, 6] Method #2 : Using list comprehension + list slicing This method is one liner alternative to perform this task.

here is a simple example using 2 for loops:

for l in initial_list:
    for j in range(0, 3*len(l), 3):
        l[j: j + 1] = [l[j]] * 3

I've been testing against @CDJB solution (with sorting):

from random import choice
def test1():
    initial_list = [[choice(range(1000)) for _ in range(1000)] for _ in range(100)] 
    def my_func(initial_list):
        for l in initial_list:
            for j in range(0, 3*len(l), 3):
                l[j: j + 1] = [l[j]] * 3
        return initial_list

    my_func(initial_list)

def test2():
    initial_list = [[choice(range(1000)) for _ in range(1000)] for _ in range(100)]  
    [sorted(x*3, key=x.index) for x in initial_list]

here are the results:

%timeit test2()
1.55 s ± 5.12 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

and:

%timeit test1()
165 ms ± 542 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

this simple solution is 9 times faster, depends on your data of course

Append vs Extend in Python Lists, Here the argument i is the index of the element before which to insert the element x . Thus, a.insert(len(a), x) is the same thing as a.append(x) . Although� Python List extend () The extend () method adds all the elements of an iterable (list, tuple, string etc.) to the end of the list. The syntax of the extend () method is: list1.extend (iterable)

You could do this using zip and chain (from itertools):

  from itertools import chain
  aList  = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 
  aList3 = [ list(chain(*(i3 for i3 in zip(*[sl]*3)))) for sl in aList ]

or using reduce from functools (which turned out to be much slower on larger lists):

  from functools import reduce
  aList  = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  aList3 = [ list(reduce(lambda a,e:a+[e]*3,sl,[]))for sl in aList ]

or zip with a nested comprehension (which is a bit faster than chain-zip):

  aList  = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  aList3 = [[i for i3 in zip(*[sl]*3) for i in i3] for sl in initial_list]

Python Lists: Append vs Extend (With Examples), Two of the most common List methods in Python are the append and extend Say you want to append an item to a list L>/span> that initially has 4 elements This above code seems to have the same effect of extending a list by another list, � This can be found out by comparing the elements in the two lists with the below approaches decribed. Using In. In a for loop we use the in clause to check in an element is present in the list or not. We will stretch this logic to compare the elements of the lists by choosing an element from first list and checking its presence in the second list.

18 Most Common Python List Questions, Discover how to create a list in Python, select list elements, the difference between append() and extend(), why to use NumPy and much more. Let's say you want to make the same list as before, but you only want to multiply� A bit longer than I expected: >>> df samples subject trial_num 0 [-0.07, -2.9, -2.44] 1 1 1 [-1.52, -0.35, 0.1] 1 2 2 [-0.17, 0.57, -0.65] 1 3 3 [-0.82, -1.06, 0.47] 2 1 4 [0.79, 1.35, -0.09] 2 2 5 [1.17, 1.14, -1.79] 2 3 >>> >>> s = df.apply(lambda x: pd.Series(x['samples']),axis=1).stack().reset_index(level=1, drop=True) >>> s.name = 'sample' >>> >>> df.drop('samples', axis=1).join(s

Related questions: Circular list iterator in Python, Duplicate elements in a list, Repeat a list within a list X number of times, Best way to extend a list with itself N times. – Georgy Oct 1 '19 at 21:00

In Python, you can expand list, tuple, and dictionarie (dict), and pass each element to function arguments. Add * to a list or tuple and ** to a dictionary when calling a function, then elements are passed to arguments.Note the number of asterisks *. Here, the following contents will be described. Expand list and tuple with * With default arguments