c = list(itertools.chain.from_iterable(zip(list_a, list_b)))
I have two list list_a and list_b
list_a has one more element than list_b and i want to insert between two elements of a one element of b.
Unfortunately this method from above deletes the last element of list_a in the result list c.
How can i fix this?
Python 2.7
I don't know enough about itertools to say much more, but if you only need to append the last element of list_a to c, why not do just that?
import itertools
list_a = [1,3,5,7,9]
list_b = [2,4,6,8]
c = list(itertools.chain.from_iterable(zip(list_a, list_b)))
# c is [1, 2, 3, 4, 5, 6, 7, 8]
c.append(list_a[-1])
# after append c is [1, 2, 3, 4, 5, 6, 7, 8, 9]
Related
This question already has answers here:
Common elements comparison between 2 lists
(14 answers)
Closed last month.
am looping big lists looking for a list of values in that big list. The bellow is a C++ tranditional way in python to find a value or a set of values, but am pretty sure that there should a better way to do it spcially using python. Any suggestions?
a better and faster way to find a list of values in another list
list_a = [1, 2, 3]
list_b = [1, 5, 4, 7, 2, 6, 8, 9]
list_resutl = []
for number_a in list_a:
for number_b in list_b:
if number_a == number_b:
list_resutl.append(number_a)
print(list_resutl)
1, 2
You can use list comprehension:
list_resutl = [item for item in list_a if item in list_b]
Code:
list_a = [1, 2, 3]
list_b = [1, 5, 4, 7, 2, 6, 8, 9]
list_resutl = [item for item in list_a if item in list_b]
print(list_resutl)
Output:
[1, 2]
You can convert each list to a set, then use set intersection.
list_result = list(set(list_a) & set(list_b))
Given multiple lists like the ones shown:
a = [1, 2, 3]
b = [5, 6, 7, 8]
c = [9, 0, 1]
d = [2, 3, 4, 5, 6, 7]
...
I want to be able to combine them to take as many elements from the first list as I can before starting to take elements from the second list, so the result would be:
result = [1, 2, 3, 8, 6, 7]
Is there a particularly nice way to write this? I can't think of a really simple one without a for loop. Maybe a list comprehension with a clever zip.
Simple slicing and concatenation:
a + b[len(a):]
Or with more lists:
res = []
for lst in (a, b, c, d):
res += lst[len(res):]
# [1, 2, 3, 8, 6, 7]
With itertools.zip_longest() for Python 3, works on any number of input lists:
>>> from itertools import zip_longest
>>> [next(x for x in t if x is not None) for t in zip_longest(a,b,c,d)]
[1, 2, 3, 8, 6, 7]
The default fill value is None so take the first none None element in each tuple created with the zip_longest call (you can change the defaults and criteria if None is a valid data value)
With functools.reduce:
from functools import reduce
print(list(reduce(lambda a, b: a + b[len(a):], [a, b, c, d])))
This outputs:
[1, 2, 3, 8, 6, 7]
I have a dictionary with varying numbers of elements. e.g.:
data = {"Tr1":[1,2,3], "Tr2": [4,5,6], "Tr3": [7,8,9]}
I would like to extract all values from the dictionary, and write it to a new separate list. e.g.:
outputData = [1,2,3,4,5,6,7,8,9]
Thanks in advance!
You could also do it with list comprehension:
>>> [i for x in data.values() for i in x]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
One way is to use dict.values() to get the lists and flatten the result using itertools.chain.from_iterable:
from itertools import chain
outputData = list(chain.from_iterable(data.values()))
print(outputData)
#[1, 2, 3, 4, 5, 6, 7, 8, 9]
I have a 2 dimensional list. Example:
list = [[1,4,7], [2,5,8], [3,6,9]]
I need to make a new list whose elements are the elements of each sub-element preserving the order.
Example:
final_list = [1,2,3,4,5,6,7,8,9]
Q: The new list must take the first element of each list first, then the
second, and so on..
A:
You can use zip and a list comprehension.
list_1 = [1,4,7]
list_2 = [2,5,8]
list_3 = [3,6,9]
print([x for lst in zip(list_1, list_2, list_3) for x in lst])
# outputs > [1, 2, 3, 4, 5, 6, 7, 8, 9]
For your new list, you could do the same, but first unpack the list with a *
new_list = [[1,4,7], [2,5,8], [3,6,9]]
print([x for lst in zip(*new_list) for x in lst])
# outputs > [1, 2, 3, 4, 5, 6, 7, 8, 9]
first transpose the list matrix with zip then chaining all the inner list together with itertools
also rename variable list to lst, not a good practice to use builtin list as variable name
import itertools
lst = [[1,4,7], [2,5,8], [3,6,9]]
final_list = list(itertools.chain.from_iterable(zip(*lst)))
# final_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
I'm new to python, and I'm trying to get to know the list comprehensions better.
I'm not even really sure if list comprehension is the word I'm looking for, since I'm not generating a list. But I am doing something similar.
This is what I am trying to do:
I have a list of numbers, the length of which is divisible by three.
So say I have nums = [1, 2, 3, 4, 5, 6]
I want to iterate over the list and get the sum of each group of three digits.
Currently I am doing this:
for i in range(0, len(nums), 3):
nsum = a + b + c for a, b, c in nums[i:i+3]
print(nsum)
I know this is wrong, but is there a way to do this? I'm sure I've overlooked something probably very simple... But I can't think of another way to do this.
See sum(iterable[, start]) builtin, and use it on slices.
Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable‘s items are normally numbers, and are not allowed to be strings.
>>> nums
[1, 2, 3, 4, 5, 6]
>>> [sum(nums[i:i+3]) for i in range(0, len(nums),3)]
[6, 15]
>>>
import itertools
nums = [1, 2, 3, 4, 5, 6]
print [a + b + c for a, b, c in itertools.izip(*[iter(nums)] * 3)]
nums = [1, 2, 3, 4, 5, 6]
map(sum, itertools.izip(*[iter(nums)]*3))