I have a list of some elements, e.g. [1, 2, 3, 4]
and a single object, e.g. 'a'
. I want to produce a list of tuples with the elements of the list in the first position and the single object in the second position: [(1, 'a'), (2, 'a'), (3, 'a'), (4, 'a')]
.
I could do it with zip
like this:
def zip_with_scalar(l, o): # l - the list; o - the object return list(zip(l, [o] * len(l)))
However, this gives me a feeling of creating and unnecessary list of repeating element.
Another possibility is
def zip_with_scalar(l, o): return [(i, o) for i in l]
which is very clean and pythonic indeed, but here I do the whole thing "manually". In Haskell I would do something like
zipWithScalar l o = zip l $ repeat o
Is there any built-in function or trick, either for the zipping with scalar or for something that would enable me to use ordinary zip, i.e. sort-of infinite list?
You don't want to mutate the list in place (eg by using . pop() ). To check that the list contains only one element, you could use a couple of try , except statements to check that (1) you can access the first element, and (2) you can't access a second element. Really, the most Pythonic way is to just use len .
To create a Python zip list of lists, define three different lists with the same number of items and pass those lists to the zip() method, which will return the tuple iterator and then convert it into the list using the list() method.
To access the first element (12) of a list, we can use the subscript syntax [ ] by passing an index 0 . In Python lists are zero-indexed, so the first element is available at index 0 . Similarly, we can also use the slicing syntax [:1] to get the first element of a list in Python.
Python's zip() function is defined as zip(*iterables) . The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.
This means, that if you pass in a list that is 5 items long, and another list that is one million items long, you’ll end up with a zip item that contains five items. Let’s verify this by zipping two lists of different lengths:
Zipping two lists in Python is a very easy thing to do. The function takes care of most things, without much user input. Essentially, the zip () function will accept any number of interables. As shown in the example above, to acces to zipped lists, we need to find ways of unpacking that list.
Since 5 is the length of the first (and shortest) range () object, zip () outputs a list of five tuples. There are still 95 unmatched elements from the second range () object. These are all ignored by zip () since there are no more elements from the first range () object to complete the pairs.
Zipping lists of unequal or different lengths results in a zip object that is as long as the shortest iterable in the items being passed in. This means, that if you pass in a list that is 5 items long, and another list that is one million items long, you’ll end up with a zip item that contains five items.
This is the cloest to your Haskell solution:
import itertools def zip_with_scalar(l, o): return zip(l, itertools.repeat(o))
You could also use generators, which avoid creating a list like comprehensions do:
def zip_with_scalar(l, o): return ((i, o) for i in l)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With