Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zip list with a single element

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?

like image 398
zegkljan Avatar asked Apr 10 '16 22:04

zegkljan


People also ask

Can a list contain only one element?

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 .

How do you zip an element in a list Python?

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.

How do you find the one element in a list?

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.

Can you zip a tuple and a list?

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.

How many items are in a 5 item ZIP file?

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:

How to zip two lists in Python?

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.

Why does zip () produce a list of 5 tuples?

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.

What happens when you Zip a list with different length?

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.


1 Answers

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) 
like image 93
kirbyfan64sos Avatar answered Oct 18 '22 15:10

kirbyfan64sos