You could use zip_longest
with a fill value (in case your list has an odd number of sublists) to zip an iterator over list1
. Running a list comprehension over the zip generator object allows you to concatenate the consecutive pairs of lists:
>>> from itertools import zip_longest # izip_longest in Python 2. x >>> x = iter(list1) >>> [a + b for a, b in zip_longest(x, x, fillvalue = []) ] [ [1, 2, 3, 4, 5], [6, 7, 8], [9, 10] ]
Try using a list comprehension (but be careful with the indexes!). It works for lists with an even or odd number of sublists:
list1 = [
[1, 2, 3],
[4, 5],
[6],
[7, 8],
[9, 10]
]
n = len(list1)
[list1[i] + (list1[i + 1]
if i + 1 < n
else []) for i in xrange(0, n, 2)] => [
[1, 2, 3, 4, 5],
[6, 7, 8],
[9, 10]
]
list1 = [
[1, 2, 3],
[4, 5],
[6],
[7, 8],
[9, 10]
]
length = len(list1)
new_list = [list1[i] + list1[i + 1]
if i + 1 < length
else [list1[i]]
for i in range(0, length, 2)
]
print(new_list)
>>> list1 = [ [1, 2, 3], [4, 5], [6], [7, 8], [9, 10] ] >>> list1[[1, 2, 3], [4, 5], [6], [7, 8], [9, 10]]
Now we can do:
>>> test = [list1[0] + list1[1]] + [list1[2] + list1[3]] + list1[4] >>> test[[1, 2, 3, 4, 5], [6, 7, 8], 9, 10] >>>
list1 = [
[1, 2, 3],
[4, 5],
[6],
[7, 8],
[9, 10]
]
from itertools
import islice, chain
print([list(chain.from_iterable(islice(list1, i, i + 2)))
for i in range(0, len(list1), 2)
])[[1, 2, 3, 4, 5], [6, 7, 8], [9, 10]]
Or without islice:
print([list(chain.from_iterable(list1[i: i + 2]))
for i in range(0, len(list1), 2)
])[[1, 2, 3, 4, 5], [6, 7, 8], [9, 10]]
Use a simple loop:
list1 = [ [1, 2, 3], [4, 5], [6], [7, 8], [9, 10] ] newlist = [] for i in range(0, len(list1), 2): newlist.append(list1[i] + list1[i + 1]) if len(list1) % 2 > 0: newlist.append(list1[-1]) print newlist
Last Updated : 18 May, 2020
The original list: [5, 4, 1, 3, 2] The consecutive element paired list is: [ [5, 4], [4, 1], [1, 3], [3, 2] ]
A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type. Lists and strings — and other things that behave like ordered sets — are called sequences.,Functions which take lists as arguments and change them during execution are called modifiers and the changes they make are called side effects.,There are several ways to create a new list; the simplest is to enclose the elements in square brackets ( [ and ]):,If there is a third argument, it specifies the space between successive values, which is called the step size. This example counts from 1 to 10 by steps of 2:
[10, 20, 30, 40]
["spam", "bungee", "swallow"]
["hello", 2.0, 5, [10, 20]]
>>>
if []:
...print 'This is true.'
...
else:
...print 'This is false.'
...
This is false. >>>
>>> vocabulary = ["ameliorate", "castigate", "defenestrate"] >>>
numbers = [17, 123] >>>
empty = [] >>>
print vocabulary, numbers, empty['ameliorate', 'castigate', 'defenestrate'][17, 123][]
>>> print numbers[0]
17
>>> numbers[9-8]
123
>>> numbers[1.0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers
If we output the element at index 3, we get:,We can also remove elements from a list by assigning an empty list to them:,After we execute these assignment statements,If you try to access or assign to an element that does not exist, you get a runtime error:
1 2
ps = [10, 20, 30, 40]
qs = ["spam", "bungee", "swallow"]
1
zs = ["hello", 2.0, 5, [10, 20]]
1 2 3 4 5
>>> vocabulary = ["apple", "cheese", "dog"] >>>
numbers = [17, 123] >>>
an_empty_list = [] >>>
print(vocabulary, numbers, an_empty_list)["apple", "cheese", "dog"][17, 123][]