All Questions

15068 views

List of lists changes reflected across sublists unexpectedly

I needed to create a list of lists in Python, so I typed the following: myList = [[1] * 4] * 3 The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Then I changed one of t...
227157 views

Proper way to make HTML nested list?

The W3 docs have a nested list example prefixed by DEPRECATED EXAMPLE:, but they never corrected it with a non-deprecated example, nor explained exactly what is wrong with the example. So which of th...
67946 views

Python append() vs. + operator on lists, why do these give different results?

Why do these two operations (append() resp. +) give different results? >>> c = [1, 2, 3] >>> c [1, 2, 3] >>> c += c >>> c [1, 2, 3, 1, 2, 3] >>> c = [1, 2...
838 views

why can't I change only a single element in a nested list in Python

I just met something really strange of Python: >>> out=[[0]*3]*3 >>> out [[0, 0, 0], [0, 0, 0], [0, 0, 0]] >>> out[0][1] 0 >>> out[0][1]=9 >>> out [[0, 9,...
1197 views

Generating sublists using multiplication ( * ) unexpected behavior

I'm sure this has been answered somewhere but I wasn't sure how to describe it. Let's say I want to create a list containing 3 empty lists, like so: lst = [[], [], []] I thought I was being all cl...
11040 views

Split a list into nested lists on a value

Say I have a list like so: [1, 4, None, 6, 9, None, 3, 9, 4 ] I decide to split this into nested lists on None, to get this: [ [ 1, 4 ], [ 6, 9 ], [ 3, 9, 4 ] ] Of course, I could have wanted to...
10889 views

Function changes list values and not variable values in Python

Let's take a simple code: y = [1,2,3] def plusOne(y): for x in range(len(y)): y[x] += 1 return y print plusOne(y), y a = 2 def plusOne2(a): a += 1 return a print plusOne...
14421 views

Converting nested list to dataframe

The goal is to convert a nested list which sometimes contain missing records into a data frame. An example of the structure when there are missing records is: str(mylist) List of 3 \$ :List of 7 ...