I have a long list I would like to break into shorter lists. I am using a list comprehension but it seems a bit long and inelegant. Is there a better way?   # z is a list z = range(99)  ##  zz should slice z into short lists with three members ##  using list comprehension I get this zz = [ z[i : i+3] for i,x in enumerate(z) if i%3 == 0 ]  # seems a bit verbose. is there a cleaner way?
740
I have a list of arbitrary length, and I need to split it up into equal size chunks and operate on it. There are some obvious ways to do this, like keeping a counter and two lists, and when the second list fills up, add it to the first list and empty the second list for the next round of data, but this is potentially extremely expensive.  I was wondering if anyone had a good solution to this for lists of any length, e.g. using generators.  I was looking for something useful in itertools but I couldn't find anything obviously useful. Might've missed it, though.  Related question:
9440
Any idea on how to check whether that list is a subset of another?  Specifically, I have  List&lt;double&gt; t1 = new List&lt;double&gt; { 1, 3, 5 }; List&lt;double&gt; t2 = new List&lt;double&gt; { 1, 5 };   How to check that t2 is a subset of t1, using LINQ?
12628
Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:  &gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2
106641
How do I format a list of questions like this?     I'm getting the equation on a new line below the i) when using enumerate and \list.  I don't want to get rid of \[ and \] either as I like the long integral sign  I get this instead:
18727
This question is somewhat of a follow up to an answer to a previous question ().  I would like to create a list environment (similar to itemize and enumerate) such that the output of the list environment is a comma-separated list.  For example, the following input:  \begin{commalist}     \item Item A     \item Item B     \item Item C \end{commalist}   Should produce the following output:  Item A, Item B and Item C.   Additionally, it would be nice to be able to specify the use of a comma instead of the final and (so that the output is instead Item A, Item B, Item C..
80273
If I have a dictionary like:  { 'a': 1, 'b': 2, 'c': 3 }   How can I convert it to this?  [ ('a', 1), ('b', 2), ('c', 3) ]   And how can I convert it to this?  [ (1, 'a'), (2, 'b'), (3, 'c') ]
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.
78277
For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines.   One quick way is to do this:  lines = len(list(open(fname)))   However, this loads the whole file into memory (at once). This rather defeats the purpose of an iterator (which only needs to keep the current line in memory).  This doesn't work:  lines = len(line for line in open(fname))   as generators don't have a length.  Is there any way to do this short of defining a count function?  def count(i):     c = 0     for el in i: c += 1     return c   To clarify, I understand that the whole file will have to be read! I just don't want it in memory all at once
8317
If I have sequence of sequences (maybe a list of tuples) I can use itertools.chain() to flatten it. But sometimes I feel like I would rather write it as a comprehension. I just can't figure out how to do it. Here's a very construed case:  Let's say I want to swap the elements of every pair in a sequence. I use a string as a sequence here:  &gt;&gt;&gt; from itertools import chain &gt;&gt;&gt; seq = '012345' &gt;&gt;&gt; swapped_pairs = zip(seq[1::2], seq[::2]) &gt;&gt;&gt; swapped_pairs [('1', '0'), ('3', '2'), ('5', '4')] &gt;&gt;&gt; "".join(chain(*swapped_pairs)) '103254'   I use zip on the even and odd slices of the sequence to swap the pairs. But I end up with a list of tuples that now need to be flattened. So I use chain(). Is there a way I could express it with a comprehension instead?  If you want to post your own solution to the basic problem of swapping elements of the pairs, go ahead, I'll up-vote anything that teaches me something new. But I will only mark as accepted an answer that is targeted on my question, even if the answer is "No, you can't.".
109666
What is the best way to copy a list? I know the following ways, which one is better? Or is there another way?  lst = ['one', 2, 3]  lst1 = list(lst)  lst2 = lst[:]  import copy lst3 = copy.copy(lst)
2624
Is there any way I can separate a List&lt;SomeObject&gt; into several separate lists of SomeObject, using the item index as the delimiter of each split?  Let me exemplify:  I have a List&lt;SomeObject&gt; and I need a List&lt;List&lt;SomeObject&gt;&gt; or List&lt;SomeObject&gt;[], so that each of these resulting lists will contain a group of 3 items of the original list (sequentially).  eg.:   Original List: [a, g, e, w, p, s, q, f, x, y, i, m, c] Resulting lists: [a, g, e], [w, p, s], [q, f, x], [y, i, m], [c]   I'd also need the resulting lists size to be a parameter of this function.
107940
In school, we were taught to end each list item with a semicolon. But I have never actually seen this. Can somebody please confirm this? Should we end each list item with anything at all?  I've seen people use periods after each list item, colons, and even commas. But I've never seen a semicolon at the end of each item — and even if I have, it would be that rare that I've forgotten.  Example:   item; item; item; item
90301
Formatting breaks if code follows a list:   List item List item  This is in a code block  This does not work as expected   However, it works if between the list and the code   List item List item   There is a line of normal text.  This is in a code block # This does work as expected   Edit  To make this clear: This is not about nesting code in a list but about code following a (finished) list not working as expected (by me). To put it differently: Why is a list not considered finished despite of following empty lines? What do I have to do?
114975
I need a list of integers from 1 to x where x is set by the user.  I could build it with a for loop eg assuming x is an integer set previously:  List&lt;int&gt; iList = new List&lt;int&gt;(); for (int i = 1; i &lt;= x; i++) {     iList.Add(i); }   This seems dumb, surely there's a more elegant way to do this, something like the
11629
Is it possible to delete multiple elements from a list at the same time? If I want to delete elements at index 0 and 2, and try something like del somelist[0], followed by del somelist[2], the second statement will actually delete somelist[3].  I suppose I could always delete the higher numbered elements first but I'm hoping there is a better way.
104167
What is the most elegant method for dumping a list in python into an sqlite3 DB as binary data (i.e., a BLOB cell)?  data = [ 0, 1, 2, 3, 4, 5 ] # now write this to db as binary data # 0000 0000 # 0000 0001 # ... # 0000 0101
57185
I want to split a character string n by n. I know how to do that 1 by 1 :  \documentclass{article} \makeatletter  \def\s@plit&lt;#1#2&gt;{%   \ifx\empty#2%     #1%   \else     #1,\s@plit&lt;#2&gt;%   \fi}  \def\Split#1#2{\s@plit&lt;#2\empty&gt;} \makeatother  \begin{document} \Split{1}{123456789} \end{document}   It gives : 1,2,3,4,5,6,7,8,9  But I also want 1,23,45,67,89 or 123,456,789 ... depending from the first parameter.  EDIT  Work straight and reverse, even with foreach.        \documentclass{article}     \usepackage{tikz,xparse,xstring}  \DeclareDocumentCommand{\split}{smmo}{%     \xdef\Entree{#3}%     \let\Sortie\empty%     \IfBooleanTF #1     {% sans étoile         \loop         \StrRight{\Entree}{#2}[\tmp]%         \edef\Sortie{\Sortie,\tmp}%         \StrGobbleRight{\Entree}{#2}[\Entree]%         \unless\ifx\Entree\empty         \repeat         \StrGobbleLeft{\Sortie}{1}[\Sortie]%     }     {% avec étoile dans l'ordre inverse         \loop         \StrRight{\Entree}{#2}[\tmp]%         \edef\Sortie{\tmp,\Sortie}%         \StrGobbleRight{\Entree}{#2}[\Entree]%         \unless\ifx\Entree\empty         \repeat         \StrGobbleRight{\Sortie}{1}[\Sortie]%     }     \IfNoValueTF{#4}{\Sortie}{\edef#4{\Sortie}}     }  \begin{document} \split{2}{6513782}  \split*{2}{6513782}  \split*{2}{6513782}[\bob]  \foreach \x in \bob {[\x] }  \end{document}