This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 5 years ago.
I have two lists:
A = ['John,Male,20,','Jenny,Female,25','James,Male,30']
B = ['London','Paris','Washington']
Is there a way I can insert the values in the second list into the first list
Hypothetical output:
['John,Male,20,London','Jenny,Female,25,Paris','James,Male,30,Washington']
Use zip to join the lists into lists of pairs. That would give you:
>>> zip(A, B)
[('John,Male,20,', 'London'), ('Jenny,Female,25', 'Paris'), 'James,Male,30', 'Washington')]
Then you can concatenate the strings in each pair using a list comprehension:
>>> [a + b for a, b in zip(A, B)]
['John,Male,20,London', 'Jenny,Female,25,Paris', 'James,Male,30,Washington']
Related
This question already has answers here:
Pass a list to a function to act as multiple arguments [duplicate]
(3 answers)
Closed 5 years ago.
If I have list of lists
A = [a, b, ..., d]
in Python, then how can I apply itertools.product to it?
I know that I can crossproduct over explicit lists
import itertools
for combination in itertools.product(a, b, ..., d):
...
but how to deal with dynamic list of lists like A?
Just unpack the function arguments from the list:
A = [a, b, ..., d]
for combination in itertools.product(*A):
...
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 years ago.
How do I convert a list of lists to a single list?
Input:
a=[['AA'], ['AE'], ['AH'], ['AO'],]
Desired output:
['AA','AE','AH','AO']
a=[['AA'], ['AE'], ['AH'], ['AO'],]
l=[]
for i in a:
l.extend(i)
print l
you could use comprehension list:
or using map and lambda functions
a=[['AA'], ['AE'], ['AH'], ['AO'],]
# open the item [0] with generators
Newa = [x[0] for x in a]
>>>['AA', 'AE', 'AH', 'AO']
see examples: http://www.secnetix.de/olli/Python/list_comprehensions.hawk
EDIT:
for i, value in enumerate(a):
a[i] = value[0]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 8 years ago.
I have a list like:
l = [[1,2,3],[4,5]]
I would like to unpack each element to make it like:
l = [1,2,3,4,5]
I have my solution here:
l = reduce(lambda x, y: x+y, l)
Anyone has other Pythonic way? Thanks.
You should use itertools methods;
from itertools import chain
l = [[1,2,3],[4,5]]
list(chain.from_iterable(l))
This question already has answers here:
Add a character to each item in a list [duplicate]
(3 answers)
Closed 9 years ago.
I have a string which I want to concatenate with every object within a list. Here is an example:
a = ['1','2']
b = 'a'
and I want:
c = ['a1','a2']
It seems that strings can't be concatenated to list objects directly so I assume that I should convert my list to the string and then add it. Is it correct or any suggestions?
Try Python list comprehensions.
>>> a = ['1','2']
>>> b = 'a'
>>> [b+i for i in a]
['a1', 'a2']
This question already has answers here:
Removing duplicates from a list of lists
(16 answers)
Closed 4 years ago.
I want to remove all duplicates list from a list of list.
So I have a list of lists like this.
a = [[1,2],[1,2],[3,4,5],[3,4,5],[3,4,5]]
I want to have:
b = [[1,2],[3,4,5]]
I don't know how to do it.
You could use a set:
b_set = set(map(tuple,a)) #need to convert the inner lists to tuples so they are hashable
b = map(list,b_set) #Now convert tuples back into lists (maybe unnecessary?)
Or, if you prefer list comprehensions/generators:
b_set = set(tuple(x) for x in a)
b = [ list(x) for x in b_set ]
Finally, if order is important, you can always sort b:
b.sort(key = lambda x: a.index(x) )
See mgilson's answer if the order of the lists is not important. If you want to retain the order, do something like:
b = list()
for sublist in a:
if sublist not in b:
b.append(sublist)
This will keep the order in the original list. However, it is slower and more verbose than using sets.