Reputation: 45
I want to be able to create a 2d list with a variable size.
test = [[]]
The problem is the data I want to put inside of it is a floating point. This makes it incompatible with the append function
TempData[0] = 1
TempData[1] = 2.32
TempData[2] = 3.65
test.append(float(TempData))
Is There any way around this? I don't really want to declare a huge list because sometimes the 2D list size may be very big or very small.
Upvotes: 1
Views: 834
Reputation: 3782
The python array module is made specifically for the purpose of holding numeric values. Here is an example using a list and an array.array
:
import array
mylist = []
mylist.append(array.array('f', [1.43, 1.54, 1.24]))
Upvotes: 0
Reputation: 1925
It looks like your issue is due to passing an object, TempData to a list and then changing the contents of that object. A reference to TempData is stored in the list, not the values contained in that list. When you alter TempData, it alters every element in the list. Instead, try this:
test = []
test.append([1, 2.32, 3.65])
test.append([2.312, 1.231, 1.111])
Upvotes: 2