Combining every pair of list items and creating a new list.

Rick Johnson rantingrickjohnson at gmail.com
Wed Jul 19 21:47:05 EDT 2017


On Tuesday, July 18, 2017 at 5:19:56 AM UTC-5, Rahul K P wrote:
> You can use a simple logic and list comprehension.
> 
> so it will be like this
> 
> lst = [1, 2, 3, 4, 5, 6, 7, 8]
> print [lst[i:i+2] for i in range(0,len(lst),2)]

No no no. Anybody can write code like that! To wow a
professor and earn a high grade, the OP must prove a
competence in mental gymnastics. Try this one liner:

# BOILER PLATE
>>> import sys
>>> from operator import add, sub, mul
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8]
# MEAT AND TATERS
>>> sys.stdout.write(str(repr([lst[i:add(map(int, tuple([i]))[0], sub(2, 0))] for i in range(range(10)[0], mul(len(lst[:]), 1), sub(2, 0))])))
[[1, 2], [3, 4], [5, 6], [7, 8]]



More information about the Python-list mailing list