I want to write a function that takes three lists of integers, l1, l2, and l3, and returns
a list of all tuples, where each tuple (a,b,c) is such that a is from l1, b is
from l2 and c is from l3 and a+b=c.
Expected Output:
[(2,5,7), (3,4,7), (3,5,8)]
The output I got:
None
Here is the code that I have written:
def threeList(l1, l2, l3):
result = []
for num in list3:
for x in list1:
for y in list2:
if x + y == num:
tuples = (x,y,num)
list_of_tuples = result.extend(tuples)
return list_of_tuples
list1 = [1,2,3]
list2 = [4,5]
list3 = [7,8]
threeList(list1, list2, list3)
Try this:
import itertools as it
list1 = [1,2,3]
list2 = [4,5]
list3 = [7,8]
[(x,y,x+y) for x,y in it.product(list1,list2) if x+y in list3]
which returns:
> [(2, 5, 7), (3, 4, 7), (3, 5, 8)]
itertools.product is a clean replacement for nested for loops.
extend is an in-place method which returns None.
Change your code to:
def threeList(l1, l2, l3):
result = []
for num in l3:
for x in l1:
for y in l2:
if x + y == num:
tuples = (x, y, num)
result.append(tuples)
return result
list1 = [1, 2, 3]
list2 = [4, 5]
list3 = [7, 8]
print(threeList(list1, list2, list3))
Output:
[(2, 5, 7), (3, 4, 7), (3, 5, 8)]
Explanation :
I used append instead of extend because we want the tuple tuples itself not the items inside it. Also I replaced the names list3, list1, list2 with l3, l1, l2 respectively because these are the actual values passed to the function, if your code works now is because list1, list2, list3 are exist in outside and those are referenced inside the function. Then after the loop finishes I return the result which is a list of tuples.
This was to fix your solution, but a better alternative solution is to use itertools modules like product in your situation.
Extend modifies the object itself. It does not return anything.
result.extend(tuples)
return result
Also note your code snippet is not working. So it is hard to spot your exact issue.
Did you make a typo?
Indentation wrong?
etc
Related
This question already has answers here:
How to merge lists into a list of tuples?
(10 answers)
How to pair up two lists? [duplicate]
(3 answers)
Closed 4 years ago.
I have two lists and I would like to create a list of lists but mainting the order, so if I have:
l1 = [1,2,3,2]
l2 = [2,3,4,1]
I would like to have:
ans = [[1,2],[2,3],[3,4],[2,1]]
It mantains the order of the indexes
Thank you!
You can use zip,
ans = [[a, b] for a, b in zip(l1, l2)]
You can find a good tutorial about zip in this tutorial.
In case one of the lists is longer then the other, you can use zip_longest (documented here):
from iterators import zip_longest
l1 = [1,2,3,2,7]
l2 = [2,3,4,1]
ans = [[a, b] for a, b in zip_longest(l1, l2, fillvalue=0)]
# output: ans = [[1,2],[2,3],[3,4],[2,1],[7,0]]
>>> l1 = [1,2,3,2]
>>> l2 = [2,3,4,1]
>>> ans = [[1,2],[2,3],[3,4],[2,1]]
>>> [list(v) for v in zip(l1, l2)]
[[1, 2], [2, 3], [3, 4], [2, 1]]
>>> assert _ == ans
You can simply do following:
l1 = [1,2,3,2]
l2 = [2,3,4,1]
ans = zip(l1, l2) #convert to tuples
ans = map(list, ans) #convert every tuples to list
for pairs in ans:
print pairs
EDIT: izip is needed for python2 only. In Python 3 the built-in zip does the same job as itertools.izip in 2.x (returns an iterator instead of a list
Using zip
l1 = [1,2,3,2]
l2 = [2,3,4,1]
zip(l1, l2)
# [(1, 2), (2, 3), (3, 4), (2, 1)]
Note that zip return list type:
type(zip(l1, l2))
# <type 'list'>
It means zip computes all the list at once, which is not memory efficient when your l1, l2 is large. For saving memory, using izip: izip computes the elements only when requested.
from itertools import izip
y = izip(l1, l2)
for item in y:
print(item)
# (1, 2), (2, 3), (3, 4), (2, 1)
print(type(y))
# <itertools.izip object at 0x7f628f485f38>
I am new to python so apologies for the naive question. I have a list
l1 = [2, 4, 6, 7, 8]
and another list of tuples
l2 = [(4,6), (6,8), (8,10)]
I want to output a list l3 of size l1 that compares the value of l1 to the first co-ordinates of l2 and stores the second co-ordinate if the first co-ordinate is found in l1, else stores 0.
output :
l3 = [0, 6, 8, 0, 10]
I tired to do a for loop like:
l3 = []
for i in range(len(l1)):
if l1[i] == l2[i][0]:
l3.append(l2[i][1])
else:
l3.append(0)
but this doesn't work. It gives the error
IndexError: list index out of range
which is obvious as l2 is shorter than l1.
You can create a dictionary from l2:
l1 = [2,4,6,7,8]
l2 =[(4,6),(6,8),(8,10)]
new_l2 = dict(l2)
l3 = [new_l2.get(i, 0) for i in l1]
Output:
l3 = [0,6,8,0,10]
I would always use Ajax1234's solution instead, but I wanted to illustrate how I would approach it using a for-loop, as you intended:
l3 = []
for elem in l1:
pairs = list(filter(lambda x: x[0] == elem, l2))
l3.append(pairs[0][1] if pairs else 0)
An alternate approach would be using next() and a list comprehension instead of filter() and a for-loop. This one is far more efficient and readable:
l3 = [next((u[1] for u in l2 if u[0] == elem), 0) for elem in l1]
I'm not sure why I get an error saying index out of range when I test this function. Can anyone please help me fix this?
def intersect_lists(L1, L2):
'''(list, list) -> list
Return L1 with items not in L2 removed.
>>> intersect_lists([1, 2, 3, 4, 5, 6], [4, 2, 6])
[2,4,6]
'''
new_list = []
for item in range(len(L1)):
if L1[item] == L2[item]:
new_list.append(L1[item])
return new_list
Use list comprehension:
def intersect_lists(L1, L2):
return [i for i in L1 if i in L2]
However, your specific error is being caused by the fact that you are iterating over the length of L1, which will ultimately lead to an index error because the length of L1 is greater than L2.
Without list comprehension:
def intersect_lists(L1, L2):
final_list = []
for i in L1:
if i in L2:
final_list.append(i)
return final_list
Or Boolean And:
list(set(L1) & set(L2))
I have a list:
list = [1, 2, 3]
I want to get this list and add a tuple so this list becomes like this
T_list = [(1,x), (2, x), (3,x)]
How do I do it?
Use a simple list comprehension to do this:
>>> your_list = [1, 2, 3]
>>> x = 100
>>> your_list_with_tuples = [(k, x) for k in your_list]
>>> your_list_with_tuples
Output
[(1, 100), (2, 100), (3, 100)]
Also, don't name your variables list, dict, etc, because they shadow the builtin types/functions.
A list comprehension would do the trick:
t_list = [(y,x) for y in original_list]
Also, the commenter is right. Don't name your lists list. It's a built in function. Using it for something else could cause problems down the line.
Here's my attempt, although I'm not an expert.
lis = [1,2,3]
tup = (4, 5, 6)
new_tup = tuple(lis)
new_lis = []
for i in range(0,3):
data = (new_tup[i],tup[i])
new_lis.append(data)
print(new_lis)
I have a list of words.
mylist = ["aus","ausser","bei","mit","noch","seit","von","zu"]
I want to turn each element in the list into a sublist and add to each a number so that each entry is indexed. So it should output
[[1, 'aus'], [2, 'ausser'], [3, 'bei'], [4, 'mit'], [5, 'noch'], [6, 'seit'], [7, 'von'], [8, 'zu']]
I know how to do such a thing with a while loop
mylist = ["aus","ausser","bei","mit","noch","seit","von","zu","aus","ausser","bei","mit","noch","seit","von","zu","aus","ausser","bei","mit","noch","seit","von","zu"]
mylist2
i=0
while i <= 10:
mylist2.append([i,mylist[i]])
i = i +1
print(mylist2)
But I want to use the following kind of structure with a for-loop:
mylist = ["aus","ausser","bei","mit","noch","seit","von","zu"]
outlist =[[1,word] for word in mylist]
print(outlist)
I'm not sure how to do that with a for-loop. Can someone explain how to do this?
If you want the inner parts to be lists then you can cast the enumerate result to a list inside a list comprehension:
>>> mylist = ["aus","ausser","bei","mit","noch","seit","von","zu"]
>>> [[idx, item] for idx, item in enumerate(mylist, 1)]
[[1, 'aus'],
[2, 'ausser'],
[3, 'bei'],
[4, 'mit'],
[5, 'noch'],
[6, 'seit'],
[7, 'von'],
[8, 'zu']]
Use enumerate
>>> mylist = ["aus","ausser","bei","mit","noch","seit","von","zu"]
>>> list(enumerate(mylist, 1))
[(1, 'aus'),
(2, 'ausser'),
(3, 'bei'),
(4, 'mit'),
(5, 'noch'),
(6, 'seit'),
(7, 'von'),
(8, 'zu')]
If you need a list of lists instead of tuples, you can do
list(map(list(enumerate(mylist, 1))))
or
[[number, word] for number, word in enumerate(mylist, 1)]
Use enumerate:
[list(element) for element in list(enumerate(mylist, 1))]
This is the method I think you are looking for.
list1 = ["aus","ausser","bei","mit","noch","seit","von","zu"]
list2 = []
for i in range(len(list1)):
list2 += [[i+1, list1[i]]]
print (list2)
Uses a for loop to go through each item in list 1 and the indexes in list 1 and the adds 1 to the index so that it doesn't start with 0.