Python: List merging method [duplicate] - python-3.x

This question already has answers here:
Pythonic way to combine (interleave, interlace, intertwine) two lists in an alternating fashion?
(26 answers)
Closed 5 years ago.
I have a question:
I have 2 lists:
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
And I want to merge them in order to have the following result:
mergedlist = [1, 4, 2, 5, 3, 6]
How can I do that?

Like this:
mergedlist = list_1 + list_2
If you want that specific order in mergedlist:
mergedlist = []
for i, entry in enumerate(list_1):
mergedlist.extend([entry, list_2[i]])

You can use chain from iter tools:
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
from itertools import chain
res = list(chain.from_iterable((list_1[x], list_2[x]) for x in range(len(list_1))))
=> [1, 4, 2, 5, 3, 6]

In my opinion, the most Pythonic way would be:
merged_list = [item for pair in zip(list_1, list_2) for item in pair]
Alternatively, you can use collections.chain as well:
merged_list = list(chain.from_iterable(zip(list_1, list_2)))

Related

How to get combination from 2 list by taking 1 element from list1 and rest of the element from list2 and so on in python

list1 = [1,1,2,4]
list2 = [2,3,5,6]
i would like get all combination like [1,3,5,6], [1,3,5,6] like all combination in python
My question is like list1=[1,2,3,4] list2=[5,6,7,8] I need to see the lists like [1,5,6,7] [5,2,7,8] meaning all possible combination of 2 list.please help
Like this IIUC:
>>> print([[i] + list2[1:] for i in list1])
[[1, 3, 5, 6], [1, 3, 5, 6], [2, 3, 5, 6], [4, 3, 5, 6]]
I think this is what you are looking for:
import itertools
list1 = [1,1,2,4]
list2 = [2,3,5,6]
combination_list = list(itertools.combinations(list1 + list2, 4))
this joins the two lists into one list (i.e, [1,1,2,4,2,3,5,6]) and takes all the 4 element combinations.

how to arrange a list from backward to forward [duplicate]

This question already has answers here:
How do I reverse a list or loop over it backwards?
(37 answers)
Closed 2 years ago.
I want to change the first element by the last, the second element by the last but one, etc..
Then I want to print this.
My list: x = [1, 2, 3, 4, 5] I want this list as: y = [5, 4, 3, 2, 1]
how can I do it in Python 3.x?
A simple way is the following:
y = x[::-1]
Use reversed such as ,
x = [1, 2, 3, 4, 5]
print(list(reversed(x)))
output:
[5, 4, 3, 2, 1]

How to transform a list of lists like [1, [2, 3, 4], 5 ] to a list [[1,2,5], [1,3,5], [1,4,5]] in Python?

I'm just starting with Python and trying to find a general solution to transform a list of lists [1, [2, 3, 4], 5 ] to a list [[1,2,5], [1,3,5], [1,4,5]] in Python.
I've tried creating some dynamic lists but not getting what i want, not even for this simple list in the example. Any help will be greatly appreciated.
inter_l = []
aba = []
v = [1, [2, 3], 4, 5, 6]
g = globals()
for elem in v:
if isinstance(elem, (list,)):
l_ln = len(elem)
indx = v.index(elem)
for i in range(0, l_ln):
g['depth_{0}'.format(i)] = [elem[i]]
inter_l.append(list(g['depth_{0}'.format(i)]))
else:
aba.append(elem)
t = aba.extend(inter_l)
w = aba.extend(inter_l)
print(v)
print(aba)
print(inter_l)
[1, [2, 3], 4, 5, 6]
[1, 4, 5, 6, [2], [3], [2], [3]]
[[2], [3]]
The easiest way would be to leverage itertools.product function, but since it expects iterables as its inputs, the input would have to be transformed a little. One way to achieve this would be something like this:
transformed = [e if isinstance(e, list) else [e] for e in v]
which converts all non-list elements into lists and then pass this transformed input to product:
list(itertools.product(*transformed))
Note, that * in front of transformed expands transformed list into positional arguments, so that instead of a single argument of type list, a list of its elements is passed instead.
The entire pipeline looks something like this:
>>> v = [1, [2, 3, 4], 5]
>>> t = [e if isinstance(e, list) else [e] for e in v]
>>> list(itertools.product(*t))
[(1, 2, 5), (1, 3, 5), (1, 4, 5)]

To make odd position in list go forward for one step

I found this code from stackoverflow ... and wondering how can I move index position that I want.
I tried to use for loop and [::1]. And by making, len(a)*[0]...I couldn't make it.
Is there any way to fix items on its position in list?
Second, without using method below, is there another way to reorder items in list?
'''
mylist=['a','b','c','d','e']
myorder=[3,2,0,1,4]
'''
a = [1,2,3,4,5,6,7]
b = ((a+a[:0:-1])*len(a))[::len(a)][:len(a)]
[1, 7, 2, 6, 3, 5, 4] <=b
[7, 1, 6, 2, 5, 3, 4] <= the result i want
Thanks in advance.
I'm not sure if this is what you want:
someList = [1,2,3,4,5,6,7]
orderedList = sorted(someList)
reversedOrderedList = orderedList[::-1]
finalList = []
for i in range(len(someList)):
if i % 2 == 0:
finalList.append(reversedOrderedList[i//2])
else:
finalList.append(orderedList[i//2])
print(finalList)
output:
[7, 1, 6, 2, 5, 3, 4]
if so, you can write it in shorter way (without reversedOrderedList):
someList = [1,2,3,4,5,6,7]
orderedList = sorted(someList)
finalList = []
for i in range(len(someList)):
if i % 2 == 0:
finalList.append(orderedList[-1-i//2])
else:
finalList.append(orderedList[i//2])
print(finalList)
and from here you can write it without if statement:
someList = [1,2,3,4,5,6,7]
orderedList = sorted(someList)
for i in range(len(someList)):
finalList.append(orderedList[((-1)**(i%2+1)-1)//2 + ((- 1)**(i%2+1))*(i//2)])
print(finalList)
It is not pretty but after that you can easily write a generator.
Zip the list with its reversed version, flatten it and take the first half:
from itertools import chain
a = [1,2,3,4,5,6,7]
b = list(chain.from_iterable(zip(a[::-1], a)))
print(b[:len(b) // 2])
Output
[7, 1, 6, 2, 5, 3, 4]

How to multiply elements in the list in Python

I've found answers that tackle the question of how to multiply with the element value, but what concerns me is how to make copies of the element itself. I have:
a = [1, 2, 3]
x = 3
b = []
I tried:
b.append(a * x)
But that gives me:
[1, 2, 3, 1, 2, 3, 1, 2, 3]
and I need:
b = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Please note I'm a programming novice. Thanks!
If you need to copy the list and not a reference to the list, you can't use *.
b = [a[:] for i in range(x)]
(a[:] creates a copy of the list.)

Resources