concat two lists in a list of lists - python

Is it possible to concat two lists in a list of lists?
From:
listA = ['a', 'b', 'c']
listB = ['A', 'B', 'C']
listFull = listA + listB
To:
print(listFull)
[['a', 'A'],['b', 'B'],['c', 'C']]

List comprehension using zip()
listFull = [list(x) for x in zip(listA, listB)]
print(listFull)
You can also use map() without looping
listFull = list(map(list, zip(listA, listB)))
[['a', 'A'],['b', 'B'],['c', 'C']]

there are several ways to achieve this.
you either need atleast 1 for loop or a map/zip combination.
both possibilities here:
listA = ['a', 'b', 'c']
listB = ['A', 'B', 'C']
#1
listFull =[]
for i in range(len(listA)):
listFull.append([listA[i],listB[i]])
print(listFull)
# output [['a', 'A'], ['b', 'B'], ['c', 'C']]
#2
listfull2 = list(map(list,zip(listA,listB)))
print(listfull2)
# output [['a', 'A'], ['b', 'B'], ['c', 'C']]

"Concatenate" is what the + does, which puts one list after another. What you're describing is called a "zip" operation, and Python has exactly that function built-in.
zip returns an iterable, so if you want a list, you can call list on it.
print(list(zip(listA, listB)))
# Output: [('a', 'A'), ('b', 'B'), ('c', 'C')]

Related

Appending an element in nested list comprehension python

My (derailed) mind would like to do the following:
list1 = [1,2,3]
list2 = ['a','b','c']
list3 = [list([a for a in list2]).append(n) for n in list1]
to output this:
[['a', 'b', 'c', '1'], ['a', 'b', 'c', '2'], ['a', 'b', 'c', '3']]
only using single line list comprehensions (yes I'm in blinded by Haskell love).
Instead it outputs a list of 3 None type items which is understandable as I'm getting the output of append 3 times.
I think there's a key python idea I'm missing here on how I could make this work (or me being completely illogical), any help would be appreciated :)
No need to over-complicate things.
>>> list1 = [1,2,3]
>>> list2 = ['a','b','c']
>>> [list2 + [x] for x in list1]
[['a', 'b', 'c', 1], ['a', 'b', 'c', 2], ['a', 'b', 'c', 3]]

Convert Set of Tuples to a List of Lists | Python

Generating unique combinations by using set() over itertools.
Outputs a set of tuples, each originating from their invocations respectively.
Instead, I now want to convert this result into a list.
import itertools
my_list = ['A', 'B', 'C']
pairs = set(itertools.combinations(my_list, 2))
print(pairs)
>>> {('A', 'C'), ('B', 'C'), ('A', 'B')}
Instead, I would like:
[['A', 'C'], ['B', 'C'], ['A', 'B']]
a =[[item for item in pair] for pair in pairs]
print(a)
returns
[['B', 'C'], ['A', 'C'], ['A', 'B']]

How to merge two lists into a list of multiple lists?

Given two lists lst1 and lst2:
lst1 = ['a']
lst2 = [['b'],
['b', 'c'],
['b', 'c', 'd']]
I'd like to merge them into a list of multiple lists with a desired output like this:
desiredList = [['a', ['b']],
['a', ['b', 'c']],
['a', ['b', 'c', 'd']]]
Here is one of my attempts that comes close using lst1 + lst2 and list.append():
lst3 = []
for elem in lst2:
new1 = lst1
new2 = elem
theNew = new1 + new2
lst3.append(theNew)
print(lst3)
#Output:
#[['a', 'b'],
#['a', 'b', 'c'],
#['a', 'b', 'c', 'd']]
Expanding on this, I thought another variation with theNew = new1.append(new2)would do the trick. But no:
lst3 = []
for elem in lst2:
new1 = lst1
new2 = elem
#print(new1 + new2)
#theNew = new1 + new2
theNew = new1.append(new2)
lst3.append(theNew)
print(lst3)
# Output:
[None, None, None]
And you'll get the same result with extend.
I guess this should be really easy, but I'm at a loss.
Thank you for any suggestions!
You could achieve your desired output with itertools.zip_longest with a fillvalue:
>>> from itertools import zip_longest
>>> list(zip_longest(lst1, lst2, fillvalue=lst1[0]))
[('a', ['b']), ('a', ['b', 'c']), ('a', ['b', 'c', 'd'])]
Or if you need a list of lists:
>>> [list(item) for item in zip_longest(lst1, lst2, fillvalue=lst1[0])]
[['a', ['b']], ['a', ['b', 'c']], ['a', ['b', 'c', 'd']]]
Note this assumes that lst1 always contains a single element as in your example.
Or you can use use append, but you need to create new copy of the lst1:
lst3 = []
for elem in lst2:
theNew = lst1[:]
theNew.append(new2)
lst3.append(theNew)
print(lst3)
from itertools import product
list(product(lst1,lst2))
>>>[('a', ['b']), ('a', ['b', 'c']), ('a', ['b', 'c', 'd'])]
[lst1 + [new] for new in lst2]
>>>[['a', ['b']], ['a', ['b', 'c']], ['a', ['b', 'c', 'd']]]
This might help
desiredlist = list(map(lambda y:[lst1,y],lst2))

List comprehension with elements appearing twice

Suppose I have list
l = ['a', 'c', 'b']
and what I want is a list where those elements appear twice, one after the other, so
['a', 'a', 'c', 'c', 'b', 'b']
and I want to do this in the most pythonic way possible.
My half solution is doing something like
[[l[i], l[i]] for i in range(len(l))]
which yields
[['a', 'a'], ['c', 'c'], ['b', 'b']]
From here, I'd have to parse (walk) the list to remove the inner lists and obtain a single flat list.
Anyone has a better idea to do this in one go? Obviously things like l * 2 wouldn't help as it gives ['a', 'c', 'b', 'a', 'c', 'b'] and I want the same elements adjacent.
l_2 = [item for item in l for i in range(n)]
Link to origin: Stackoverflow: Repeating elements of a list n times
Using only list comprehension, you can do:
[i for j in my_list for i in [j]*2]
Output:
>>> my_list = ['a', 'c', 'b']
>>> [i for j in my_list for i in [j]*2]
['a', 'a', 'c', 'c', 'b', 'b']
You can zip the list against itself, then flatten it in a list comprehension.
>>> [i for j in zip(l,l) for i in j]
['a', 'a', 'c', 'c', 'b', 'b']
You can use zip function
l = ['a', 'c', 'b']
a = [i for j in zip(l,l) for i in j]
print(a)
Output
['a', 'a', 'c', 'c', 'b', 'b']
More general:
def ntimes(iterable, times=2):
for elt in iterable:
for _ in range(times):
yield elt
Here is a short solution without list comprehension, using the intuitive idea l*2:
sorted(l*2, key=l.index)
#['a', 'a', 'c', 'c', 'b', 'b']
If you like functional approaches, you can do this:
from itertools import chain, tee
l = ['a', 'c', 'b']
n = 2
list(chain.from_iterable(zip(*tee(l, n))))
While this might not perform as fast as the other answers, it can easily be used for arbitrary iterables (especially when they are infite or when you don't know when they end) by omitting list().
(Note that some of the other answers can also be adapted for arbitrary iterables by replacing their list comprehension by a generator expression.)

How can I make a list from a list of tuples ?

if I have a list of tuple like:
L=[(('a','b','c','d'),2),(('f','e','d','a'),3)]
I want to make a list that like:
L1=[['a','b','c','d'],['f','e','d','a']]
this is what I did:
L1=[]
for item in L:
for(letter,integer) in item:
L1.append(list(letter))
print(L1)
but it comes up with error say that there are too many values to unpack
what's wrong with my code?
What's useful here is a list comprehension:
L1 = [list(letters) for (letters, number) in L]
This iterates over each pair in your list, taking the letters tuple of each pair and converting it to a list. It then stores each result as the element of a new list.
You did:
for item in L:
for(letter,integer) in item:
L1.append(list(letter))
print(L1)
However, item is already a tuple and there's no need to iterate through it. What you want is
for letter, integer in L:
L1.append(list(letter))
Or...
for item in L:
letter, integer = item
L1.append(list(letter))
you may try:
L1 = [list(i[0]) for i in L]
Your problem is in this line:
for(letter,integer) in item:
This will (try to) iterate over item, which is a 2-element tuple. Replace it by:
(letter,integer) = item
And it will work. There are more concise ways of doing that, of course, but this should get you started.
There are several options to do that, one of them is to map your list by getting first element, eg. like this:
>>> from operator import itemgetter
>>> map(list, map(itemgetter(0), L))
[['a', 'b', 'c', 'd'], ['f', 'e', 'd', 'a']]
The other one is to use list comprehensions:
>>> [list(item[0]) for item in L]
[['a', 'b', 'c', 'd'], ['f', 'e', 'd', 'a']]
or lambda:
>>> map(lambda x: list(x[0]), L)
[['a', 'b', 'c', 'd'], ['f', 'e', 'd', 'a']]
But maybe you do not need a list and tuple will do. In such case the examples are simpler:
>>> from operator import itemgetter
>>> map(itemgetter(0), L)
[('a', 'b', 'c', 'd'), ('f', 'e', 'd', 'a')]
>>> [item[0] for item in L]
[('a', 'b', 'c', 'd'), ('f', 'e', 'd', 'a')]
>>> map(lambda x: x[0], L)
[('a', 'b', 'c', 'd'), ('f', 'e', 'd', 'a')]

Categories