Accelerating list concatenation in python 3 [duplicate] - python

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 4 years ago.
I'm running the following code:
x = []
for i in c:
x = x+i
The result has about 50-100 million elements.
This takes several minutes to run on my PC. How can I accelerate this?

Already compared in join list of lists in python
with Python 2 being faster with .extend than with itertools.chain
An exotique method
l = []
for x in c: l[0:0] = x
among the faster according to
stackoverflow.com/questions/12088089/…
For python 3.5 and latter, even more exotic
l = []
for x in c:
l = [l, *x]
Of course sum(c, []) is among worst on all the measurements.

Related

Python Reversing List Using Slicing [duplicate]

This question already has answers here:
Python reverse-stride slicing
(8 answers)
Closed 3 years ago.
I am trying to create a function that reverses and mutates a list via slicing/appending without using [::-1] or .reverse(). I am looking for any additional resources online but it seems to be the only two popular reserving techniques.
Can anyone help me think of how I can write this?
I would try:
n = len(a)
for i in range(n):
a.append(a[n - (i+1)])
a = a[n:]
This is one way you can do it:
a = [1, 2, 3, 4]
b = [0]*len(a)
for i in range(len(a)):
b[len(a) - 1 - i] = a[i]
print(b)

why does this list subtraction loop not remove both b and c? [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 3 years ago.
I have a list in python 2.7 and I want to remove any items from it that exist in another list. The loop seems to stop short and I am not sure why it only deletes b and not b and c from lt_sub?
bad = ['b','c']
lt = ['a','b','c']
lt_sub = lt
for l in lt:
if l in bad:
lt_sub.remove(l)
print lt_sub
When you assign lt_sub = lt as so, you are creating a reference to lt, not a copy. When you iterate through lt in the for loop, once you remove l, lt has no more entries in the loop (as it has now been reduced to a size of 2, and you have iterated twice).
To fix, have lt_sub be a separate list, see here for an explanation.

How to iterate over a list two at a time in Python 3? [duplicate]

This question already has answers here:
Skipping every other element after the first [duplicate]
(15 answers)
Extract elements of list at odd positions
(5 answers)
pythonic way to iterate over part of a list
(8 answers)
Closed 4 years ago.
I am trying iterate over a list, two at a time. This is my code:
list_1 = [1,3,2,4,3,1,2,7]
The ouput should be like this(the iteration should start from the first element):
1
2
3
2
Seven, is not there because the iteration in only 2.
This is my try:
nums = [1,3,2,4,3,1,2,7]
for x, y in zip(*[iter(nums)]*2):
print(x, y)
But my output is:
1 3
2 4
3 1
2 7
How can I achieve the proper iteration using Python 3?
You can use range like this by using step (indexing):
list_1 = [1,3,2,4,3,1,2,7]
for i in range(0,len(list_1),2):
print(list_1[i])
or just using python slice notation:
list_1 = [1,3,2,4,3,1,2,7]
for v in list_1[::2]:
print(v)

Getting elements from iterable like a shiftregister [duplicate]

This question already has answers here:
How can I iterate over overlapping (current, next) pairs of values from a list?
(12 answers)
Iterating over every two elements in a list [duplicate]
(22 answers)
Closed 5 years ago.
Is there an idiomatic way of taking two elements from an iterable like from a shift register? I have seen this, but it's another case. For the following example:
for i, j in something_i_need(range(5)):
print("%d %d" % (i, j))
I expect the output:
0 1
1 2
2 3
3 4
I know I can perform this using:
def shift_register(it):
it = iter(it)
x = next(it)
for y in it:
yield x, y
x = y
But i started wondering whether the python standard library contains something for this common use case so I don't have to reinvent the wheel.
In my usecase the iterator is infinite.

Functionality of list comprehension with two for loops [duplicate]

This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 6 years ago.
I was reading through an article explaining list comprehensions and came across the following example which is supposed to build a list of non-prime numbers:
noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
I tried breaking the list comprehension down by running both for loops separately in a shell, but I'm still unclear as to the functionality of the statement. It seems that the first loop is supposed to be iterating through the list of numbers from 2 to 8 and then storing each number in j which is then passed to the second (nested?) loop which generates numbers from the current value of i times 2 until 50 in increments of i.
What I've described the actual functionality of the list comprehension?
This list comprehension performs the same as the following code:
noprimes = []
for i in range(2,8):
for j in range(i*2, 50, i):
noprimes.append(j)

Categories

Resources