# Is there a difference between in pairs and ipairs?

Is there a difference between in pairs and ipairs? I’m always using in pairs, is ipairs faster or something like that?

There is already a topic with a solution on this,

1 Like

there is no difference except the fact that ipairs stops the loop when there is a nil value, lemme show you an example

``````local Table  = {"hi","Hello","welcome",nil}

for _,v in pairs(Table) do
print(v)
end
``````

Output:

``````hi
hello
welcome
nil
``````

and than the ipairs

``````local Table  = {"hi","Hello","welcome",nil}

for _,v in ipairs(Table) do
print(v)
end
``````

output:

``````hi
hello
welcome
``````

it just stops and doesn’t continue when it hits a nil value

5 Likes

Also, pairs iterates over all indexes and keys, while ipairs only works with indexed items. It starts at 1 and stops once it reaches an index that’s nil.

``````> t = {'a', 'b'}
> t['key1'] = 3
> t['key2'] = 4
> t[5] = 5
> for k, v in pairs(t) do print(k, v) end
1	a
2	b
key1	3
5	5
key2	4
> for k, v in ipairs(t) do print(k, v) end
1	a
2	b
``````
2 Likes