If you want to convert each of the lists to a list of integers, you can do
a, b = ['1','2','3'], ['1','2']
print(list(map(lambda x: map(int,x), [a, b])))
# [[1, 2, 3], [1, 2]]
which can be assigned to a and b back, like this
a, b = map(lambda x: map(int,x), [a, b])
If you want to chain the elements, you can use itertools.chain, like this
from itertools import chain
print(list(map(int, chain(a,b))))
# [1, 2, 3, 1, 2]
Edit: if you want to pass more than iterable as arguments, then the function also has to accept that many number of parameters. For example,
a, b = [1, 2, 3], [1, 2, 3]
print(list(map(lambda x, y: x + y, a, b)))
# [2, 4, 6]
If we are passing three iterables, the function has to accept three parameters,
a, b, c = [1, 2, 3], [1, 2, 3], [1, 2, 3]
print(list(map(lambda x, y, z: x + y + z, a, b, c)))
# [3, 6, 9]
If the iterables are not of the same size, then the length of the least sized iterable will be taken in to consideration. So
a, b, c = [1, 2, 3], [1, 2, 3], [1, 2]
print(list(map(lambda x, y, z: x + y + z, a, b, c)))
# [3, 6]
Answer from thefourtheye on Stack Overflowmultiple iterators for map() function in python? - Stack Overflow
Python's filter(): Extract Values From Iterables โ Real Python
Is there a better functional way to map an iterable of iterables?
Iterable... but how many times?
What does map() do in Python?
Is map() lazy in Python?
What happens when map() receives different iterable lengths?
ranges = [range(2), range(4), range(1)]
# I want to map a function onto the inner iterables. In this case, str.
list_comps = [[str(n) for n in r] for r in ranges]
gen_exps = ((str(n) for n in r) for r in ranges)
maps = map(lambda r: map(str, r), ranges) # Is there a better way?
expected = [['0', '1'], ['0', '1', '2', '3'], ['0']]
# Test
for mapped in [list_comps, gen_exps, maps]:
actual = list(map(list, mapped))
assert expected == actual, actualEdit: I'm going for a generator of generators. I'd rather not precompute into a list/tuple for memory and shortcutting.
Edit2: Summary for people of the future.(map(str, r) for r in ranges) is my new favorite. It's from this comment by u/MKuranowski. There's also map(map, repeat(str), ranges).
I realized this is just a specific case of a more general problem: that I love map for functions with one argument and dislike map for functions with more than one argument.
numbers = ['1', '2', '3'] sum(map(int, numbers)) # I like that a lot sum(int(n) for n in numbers)) # I like that less.
On the generator expression, it's the n that bothers me. It feels like that variable isn't really do anything. Things change when the function needs more than one variable.
bins = ['0', '1', '101] sum(map(lambda n: int(n, base=2), bins)) # meh sum(int(n, base=2) for n in bins)) # better
Now that n has meaning. It's showing that it's the first argument to int. Multiple iterables for arguments is interesting.
bases = [2, 3, 4] exponents = [0, 1, 2] sum(map(pow, bases, exponents)) sum(pow(a, b) for a, b in zip(bases, exponents))
If they're already in tuples, then I think itertools.starmap wins.
zipped = list(zip(bases, exponents)) sum(starmap(pow, zipped)) sum(pow(*x) for x in zipped)
You can treat a repeated argument like an iterable with itertools.repeat. This reminds me of writing Clojure.
bins = ['0', '1', '101] sum(map(int, bins, repeat(2))
Now back to the original idea. map is a function with multiple arguments. So it's not the best candidate as a function to pass to map. Instead, we end up mixing genexps and maps. I don't have a tiny example in mind for what to use this generator of generators for, so it won't be passed to anything else like in the examples above.
ranges = [range(2), range(4), range(1)] (map(str, r) for r in ranges)
But after writing all this out, I may just go full functional.
map(map, repeat(str), ranges)
And as a random side-note, I just realized you can use itertools.cycle and map to change arguments based on the modulo of the index.
from operator import add
map(add, 'XYZ', cycle('AB')) # -> ('XA', 'YB', 'ZA')
Map with multiple iterators:
Given your desired intermediate output I'd say that map isn't the right tool to get a j containing the integers 1-9.
That's because map with multiple iterators goes through the iterators simultaneously:

It doesn't repeat, that's just because it's a gif file.
The problem(s) in your approaches:
In the first iteration it will return "1", "4", "7" (the first elements of each iterable) the next iteration will return "2", "5", "8" and the last iteration "3", "6", "9".
On each of these returns it will apply the function, in your first example in the first iteration that's
int("1") and int("4") and int("7")
which evaluates to 7 because that's the last truthy value of the chained ands:
>>> int("1") and int("4") and int("7")
7
That also explains why the result is 24 because the results of the other iterations are 8 and 9:
>>> int("2") and int("5") and int("8")
8
>>> int("3") and int("6") and int("9")
9
>>> 7 + 8 + 9
24
In your second example you added the strings (which concatenates the strings) and then converted it to an integer:
>>> "1" + "4" + "7"
"147"
>>> int("147")
147
The solution:
So, you need the addition from your second approach but apply the int to each variable like you did in the first example:
j = list(map(lambda x, y, z: int(x)+int(y)+int(z), num, num2, num3))
A better solution:
But for that problem I would probably use a different approach, especially if you want the "desired" j.
To get that you need to chain the iterables:
import itertools
chained = itertools.chain(num, num2, num3)
Then convert all of them to integers:
chained_integers = map(int, chained)
This chained_integers is the iterator-equivalent to the [1, 2, 3, 4, 5, 6, 7, 8, 9] list you wanted as j. You could also use chained_integers = list(map(int, chained)) and print the chained_integers before proceeding if you want to double-check that.
And finally to reduce it I would actually use the built-in sum function:
reduced = sum(chained_integers) # or "reduce(lambda x, y: x+y, chained_integers)"
Or the one-line-version:
sum(map(int, itertools.chain(num, num2, num3)))
An alternative solution using a comprehension instead of map:
Even simpler would be a comprehension (in this case I used a generator expression) instead of the map:
reduced = sum(int(v) for v in itertools.chain(num, num2, num3))
An alternative solution using a generator function:
That's pretty short and easy to understand but I would like to present another example of how to do it using your own generator function:
def chain_as_ints(*iterables):
for iterable in iterables:
for item in iterable:
yield int(item)
And you could use it like this:
sum(chain_as_ints(num, num2, num3))
In this case a generator function is not really necessary (and probably not advisable given the alternatives) I just wanted to mention it for completeness.
Since you want to iterate over the elements of each list as one long list, it is easiest to just concatenate them:
nums = map(int, num + num2 + num3)