Python vs Lua, data structure

June 28th, 2008 mysurface Posted in Developer, Lua, python | Hits: 100320 | 8 Comments »

In python, we have various type of data structure, such as list, set, tuple, dictionary etc, but in Lua, we only have table. Table in Lua can be used as array, list, dictionary or object.

Let see how you we construct list from Lua table.

t = { 'a','b','c','d','e','f' }

To print out the whole list, we need general for in Lua:

for i,v in pairs(t) do
	print (i," = ", v)

Lua Results:

1        =      a
2        =      b
3        =      c
4        =      d
5        =      e
6        =      f

To create a list from python:

t = [ 'a','b','c','d','e','f' ]

Print out the list with the index:

for i in t:
    print t.index(i)," = ", i

Python Results:

0  =  a
1  =  b
2  =  c
3  =  d
4  =  e
5  =  f

Observed Lua and Python results, Lua list index start from 1, which means t[1] will be the first element in the list, where in python, index start from ZERO, so accessing first element in python with t[0].

Anyway, you can force Lua table start index from ZERO like this:

t = { [0]='a','b','c','d','e','f' }

But when you print out the result using for loop, it seems to be weird, the indexing does not start with ZERO but it will place ZERO at last slot.

1        =      b
2        =      c
3        =      d
4        =      e
5        =      f
0        =      a

I raise up this in Lua mailing list, Duncan replied, he provides a solution to cater this. He suggest creates another function based on ipairs like this:

local ipairsaux = ipairs({})
function ipairs2(t,i)
   return ipairsaux, t, (i or 1) - 1

t = { [0]='a','b','c','d','e','f' }
for i,v in ipairs2(t,0) do
   print (i," = ", v)

Lua’s table can be used as dictionary, lets see:

d = {a=1,b=2,c=3}

To further add in elements, you can do this:


Let print out all the elements and value through for loop.

for e,v in pairs(d) do

a       1
q       100
c       3
b       2

Add another element and print them again:

for e,v in pairs(d) do

q       100
c       3
b       2
j       345
a       1

It seems, the elements will be arrange by random.

For dictionary in python:

d= { 'a':1, 'b':2, 'c':3 }
for a in d:
    print a,d[a]
a 1
c 3
b 2


for a in d:
    print a,d[a]
a 1
q 200
c 3
b 2


for a in d:
    print a,d[a]
a 1
q 200
c 3
b 2
j 20

But amazing part of Lua’s dictionary can be access like this:


Which it is identical to print(d['a']).

In python, you can’t do that, unless you define it as a class.

class D:
print d.a

But object ‘d’ in python does not support for loop like dictionary. I might be wrong about this, because I see something like this in django.

8 Responses to “Python vs Lua, data structure”

  1. I stumbled across your post here..

    What is the point of this comparison?
    Are you saying this Lua feature should be in Python?

  2. could iterate another way in Lua when order is meaningful and start with 0
    for i=0,#t+1 do print(i,’=’,t[i]) end

  3. You don’t need to use t.index(i), Python has its own function for returning index, value pairs of a list called enumerate()

    >>> for i, v in enumerate(t):
    print i, “=”, v
    0 = a
    1 = b
    2 = c
    3 = d
    4 = e
    5 = f

  4. Luis González Says:

    for looping through the “d” object, you can do as follows:

    for i,v in d.__dict__.items():
    print i, ‘=’, v

  5. There are certainly a variety of particulars like that to take into consideration. That could be a nice point to deliver up. I offer the ideas above as normal inspiration however clearly there are questions just like the one you deliver up the place the most important thing can be working in honest good faith. I don?t know if greatest practices have emerged around issues like that, however I am positive that your job is clearly identified as a good game. Both boys and girls feel the impact of just a moment? pleasure, for the rest of their lives.

  6. www retailmenot com view themeforest net

  7. I see you don’t monetize your website, don’t waste your
    traffic, you can earn additional bucks every month
    because you’ve got high quality content. If you want to know how to make extra money, search for:
    Boorfe’s tips best adsense alternative

  8. I have checked your website and i’ve found some duplicate content, that’s why you don’t rank high in google’s search results, but
    there is a tool that can help you to create 100% unique articles,
    search for: Boorfe’s tips unlimited content

Leave a Reply