2

I want to create a fixed size list of size 6 of tuples in Python. Note that in the code below i am always re-initializing the values in the outer for loop in order to reset the previously created list which was already added to globalList. Here is a snippet:

for i,j in dictCaseId.iteritems():
    listItems=[None]*6
    for x,y in j:
        if x=='cond':
            tuppo = y
            listItems.insert(0,tuppo)
        if x=='act':
            tuppo = y
            listItems.insert(1,tuppo)
        if x=='correc':
            tuppo = y
            listItems.insert(2,tuppo)
            ...
            ...
    globalList.append(listItems)

But when I try to run the above (snippet only shown above) it increases the list size. I mean, stuff gets added but I also see the list contains more number of elements. I dont want my list size to increase and my list is a list of 6 tuples.

For example:

Initially: [None,None,None,None,None,None]
What I desire: [Mark,None,Jon,None,None,None]
What I get: [Mark,None,Jon,None,None,None,None,None]
Yavar
  • 11,883
  • 5
  • 32
  • 63

4 Answers4

4

Instead of inserting you should assign those values. list.insert inserts a new element at the index passed to it, so length of list increases by 1 after each insert operation. On the other hand assignment modifies the value at a particular index, so length remains constant.

for i,j in dictCaseId.iteritems():
    listItems=[None]*6
    for x,y in j:
        if x=='cond':
            tuppo = y
            listItems[0]=tuppo
        if x=='act':
            tuppo = y
            listItems[1]=tuppo
        if x=='correc':
            tuppo = y
            listItems[2]=tuppo

Example:

>>> lis = [None]*6
>>> lis[1] = "foo"   #change the 2nd element
>>> lis[4] = "bar"   #change the fifth element
>>> lis
[None, 'foo', None, None, 'bar', None]

Update:

>>> lis = [[] for _ in xrange(6)] # don't use  [[]]*6
>>> lis[1].append("foo")
>>> lis[4].append("bar")
>>> lis[1].append("py")
>>> lis
[[], ['foo', 'py'], [], [], ['bar'], []]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Thanks Ashwini. That works. However my list is a list of tuples. What I mean is it has 6 elements and each element can be a tuple. But when I insert like above it will rewrite the previously inserted element. So how can I insert a tuple. For example I need [(foo,bar),None,bar,None,None,None] – Yavar May 07 '13 at 09:16
  • @Yavar you may have to use a list of lists then instead of lists of None, see my updated answer. – Ashwini Chaudhary May 07 '13 at 09:19
  • 1
    Thanks buddy! That's perfect. I am a C++ coder new to Python, learning on the job. That really helped me!! Accepted with a +1 :) – Yavar May 07 '13 at 09:22
  • @Yavar You're welcome. Make sure you don't use something like this : `[[]]*6`. Reason : http://stackoverflow.com/questions/13058458/python-list-index – Ashwini Chaudhary May 07 '13 at 09:24
3

Ashwini fixes your main issue, however I will make a suggestion here.

Because (from what you have shown us) you are simply assigning an element to a specific index based on a condition, it would be better to do something like this:

for i, j in dictCaseId.iteritems():
    listItems = [None] * 6
    lst = ['cond', 'act', 'correc', ...]
    for x, y in j:
        idx = lst.index(x)
        listItems[idx] = y
    globalList.append(listItems)

Or with a list of lists:

for i, j in dictCaseId.iteritems():
    listItems = [[] for _ in xrange(6)]
    lst = ['cond', 'act', 'correc', ...]
    for x, y in j:
        idx = lst.index(x)
        listItems[idx].append(y)
    globalList.append(listItems)

This allows each of the conditions to be dealt with in one go, and it condenses your code significantly.

Community
  • 1
  • 1
Volatility
  • 31,232
  • 10
  • 80
  • 89
1

Instead of listItems.insert(0, tuppo), do listItems[0] = tuppo etc.

pts
  • 80,836
  • 20
  • 110
  • 183
1

thats becauce you insert the names indestead of changing the value. do like this indstead

for i,j in dictCaseId.iteritems():
    listItems=[None]*6
    for x,y in j:
        if x=='cond':
            tuppo = y
            listItems[0] = tuppo
        if x=='act':
            tuppo = y
            listItems[1] = tuppo
        if x=='correc':
            tuppo = y
            listItems[2] = tuppo                ...
            ...
    globalList.append(listItems)
Rasmus Damgaard Nielsen
  • 1,940
  • 4
  • 18
  • 33