I am having some trouble to understand what is happening with the following code: &gt;&gt;&gt; print(list(zip(*[iter((1,2,3,4,5,6,7,8))]*4))) [(1, 2, 3, 4), (5, 6, 7, 8)] &gt;&gt;&gt; print(list(zip(*[iter((1,2,3,4,5,6,7))]*4))) [(1, 2, 3, 4)]  I suspect it is doing internally something like: &gt;&gt;&gt; print(list(zip(*((1,5), (2,6), (3,7), (4,8))))) [(1, 2, 3, 4), (5, 6, 7, 8)]  But I can't understand how this list of iterator multiplied by a constant gives this. Could you clarify this for me?
17043
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time.  Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples.  Currently, I'm iterating over it this way: for i in range(0, len(ints), 4):     # dummy op for example code     foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]  It looks a lot like &quot;C-think&quot;, though, which makes me suspect there's a more pythonic way of dealing with this situation.  The list is discarded after iterating, so it needn't be preserved.  Perhaps something like this would be better? while ints:     foo += ints[0] * ints[1] + ints[2] * ints[3]     ints[0:4] = []  Still doesn't quite &quot;feel&quot; right, though.  :-/ Related question:
113672
For example:  &gt;&gt;&gt; x = [1, 1, 2, 'a', 'a', 3] &gt;&gt;&gt; unique(x) [1, 2, 'a', 3]   Assume list elements are hashable.  Clarification: The result should keep the first duplicate in the list. For example, [1, 2, 3, 2, 3, 1] becomes [1, 2, 3].
70618
So I was writing up a simple binary tree in Python and came across [...]  I don't believe this to be related to the Ellipsis object, more it seems to have something to do with an infinity loop (due to Python's shallow copy?). The source of this infinity loop and why it doesn't get expanded while expanding when accessed is something I'm completely lost to, however   &gt;&gt;&gt; a [[[[[], [], 8, 3], [[], [], 3, 2], 6, 3], [], 1, 4], [[], [], -4, 2], 0, 0] &gt;&gt;&gt; Keys(a)#With a+b [0, 1, 6, 8, 3, -4] &gt;&gt;&gt; Keys(a)#With [a,b] [8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...]] &gt;&gt;&gt; Keys(a)[1]#?? [8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...], 8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0, [...], [...]]     Version using a+b  def Keys(x,y=[]):     if len(x):y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y)#Though it seems I was using y=y[:]+, this actually outputs an ugly mess     return y   version using [a,b]  def Keys(x,y=[]):     if len(x):y+=[x[2],Keys(x[0],y),Keys(x[1],y)]     return y   So what exactly is [...]?
106335
What does the following mean?  Class.Function(variable := 1 + 1)   What is this operator called, and what does it do?
2695
Some of you may recognize this as Project Euler's problem number 11. The one with the grid.  I'm trying to replicate the grid in a large multidimensional array, But it's giving me a syntax error and i'm not sure why  grid = [  [ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08 ], [ 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00 ], [ 81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65 ], ...   And I get this error:     File "D:\development\Python\ProjectEuler\p11.py", line 3     [ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91 , 08 ],        ^ SyntaxError: invalid token   Why is it throwing an error before the comma?
27093
I occasionally see the list slice syntax used in Python code like this:  newList = oldList[:]   Surely this is just the same as:  newList = oldList   Or am I missing something?
121693
To do the equivalent of Python list comprehensions, I'm doing the following:  some_array.select{|x| x % 2 == 0 }.collect{|x| x * 3}   Is there a better way to do this...perhaps with one method call?
115699
I think I get it but I wanted to be sure before attempting the question:  Question  Describe the following set, and prove your answers correct. (Here brackets denote intervals on $\mathbb{R}$.)  $\bigcup_{i=0}^{\infty}[i, i+2]$  Does it mean: {$0, 1, 2, 3, 4, 5...,\infty$} $\cup$ {$2, 3, 4, 5, 6, 7,...,\infty$}?  If so, what is the difference between:  $\bigcup_{i=0}^{\infty}[i, i+2]$ and $\bigcup_{i=0}^{\infty}(i, i+2)$
37453
I have a list of tuples like this:  [ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ]   I want to iterate through this keying by the first item, so, for example, I could print something like this:  a 1 2 3 b 1 2 c 1   How would I go about doing this without keeping an item to track whether the first item is the same as I loop around the tuples? This feels rather messy (plus I have to sort the list to start with)...
1520
I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] and makes it into iterable list of tuples, like so: [(0,3), (1,7), (2,19)] to use it instead of:  for i in range(len(name_of_list)):     name_of_list[i] = something   but I can't remember the name and googling "iterate list" gets nothing.
27564
The code below generates:    What I expect is:    Why does the \foreach seem to treat the entire list as one value? How do I fix it?  \documentclass{article} \usepackage{xparse} \usepackage{xstring} \usepackage{etoolbox} \usepackage{expl3} \usepackage{tikz}  \begin{document}  \ExplSyntaxOn  \DeclareDocumentCommand{\separate}{m} {   \StrLeft{#1}{1}[\head]   \StrBehind[1]{#1}{\head}[\tail]   \IfInteger{\tail}{\head,\separate{\tail}}{\head} }  \DeclareDocumentCommand{\map}{m} {   \def\list{#1}   \foreach \i in \list   {     [\i]   } }  \ExplSyntaxOff  \def\arg{3669887}  \arg  \separate{\arg}  \map{\separate{\arg}}  \end{document}
28573
I want to make an enumerated list in the following format:  [1] foo foo [2] foo foo ...    How I can do that using enumerated lists? I have tried the following:  \begin{enumerate}[\left[ 1 \right]]   but it does not work.
2689
What is the idiomatic Python equivalent of this C/C++ code?  void foo() {     static int counter = 0;     counter++;     printf("counter is %d\n", counter); }   specifically, how does one implement the static member at the function level, as opposed to the class level? And does placing the function into a class change anything?
103660
We know that the multiplicative operator is construct by iterating the additive one, and the power operator is construct by iterating the multiplicative one.   Which makes us wonder if we can construct a decent operator, lets name it $\triangle$ (triangle) so that the addition would be an iteration of triangle, i.e. $$\underbrace{a\triangle \cdots \triangle a}_{n \text{ times}}=a+n.$$  May be such a construction would have to take place in $\mathbb R\cup \{-\infty\}$, so that $-\infty$ could be the identity element for $\triangle$. May be not.  May be one can use the natural logarithme or the exponential which can upgrade or downgrade an operator in some way.  Anyway, i just want to know any ideas you may have about that kind of operator.
467
How do you generate all the permutations of a list in Python, independently of the type of elements in that list?  For example:  permutations([]) []  permutations([1]) [1]  permutations([1, 2]) [1, 2] [2, 1]  permutations([1, 2, 3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1]
116723
List = 1 2 3 4 5 for i in $LIST do sum=`expr $l_$i + $b` done   here l_1, l_2, l_3, l_4 and l_5 are variables having some values. when I am trying to access those values within the for loop using loop variable i, the value of i is getting substituted in the expression i.e the statement (1 + $b) is getting executed instead of ($l_1 + $b). I have tried various combinations of $ symbol and also used paranthesis but nothing seems to be working. Please help!!
1083
Is there a simple way to flatten a list of iterables with a list comprehension, or failing that, what would you all consider to be the best way to flatten a shallow list like this, balancing performance and readability?  I tried to flatten such a list with a nested list comprehension, like this:  [image for image in menuitem for menuitem in list_of_menuitems]   But I get in trouble of the NameError variety there, because the name 'menuitem' is not defined.  After googling and looking around on Stack Overflow, I got the desired results with a reduce statement:  reduce(list.__add__, map(lambda x: list(x), list_of_menuitems))   But this method is fairly unreadable because I need that list(x) call there because x is a Django QuerySet object.  Conclusion:   Thanks to everyone who contributed to this question.  Here is a summary of what I learned.  I'm also making this a community wiki in case others want to add to or correct these observations.  My original reduce statement is redundant and is better written this way:  &gt;&gt;&gt; reduce(list.__add__, (list(mi) for mi in list_of_menuitems))   This is the correct syntax for a nested list comprehension (Brilliant summary !):  &gt;&gt;&gt; [image for mi in list_of_menuitems for image in mi]   But neither of these methods are as efficient as using itertools.chain:  &gt;&gt;&gt; from itertools import chain &gt;&gt;&gt; list(chain(*list_of_menuitems))   And as @cdleary notes, it's probably better style to avoid * operator magic by using chain.from_iterable like so:  &gt;&gt;&gt; chain = itertools.chain.from_iterable([[1,2],[3],[5,89],[],[6]]) &gt;&gt;&gt; print(list(chain)) &gt;&gt;&gt; [1, 2, 3, 5, 89, 6]