lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 158 forks source link

Enhance Data Structures : Support `for in` iteration of `dict` #1881

Open faze-geek opened 1 year ago

faze-geek commented 1 year ago
def f():
    d:dict[i32,i32] = {1 : 100,2 : 200,3 : 300,4 : 400}
    i:i32
    for i in d:      # for in works with string, list, tuple, set
        print(i) 
f()

(lp) C:\Users\kunni\lpython>python try.py
1
2
3
4
czgdp1807 commented 1 year ago

First we have to implement dict.keys and dict.values (1 PR). And then we can implement for in for dict (and then a separate PR).

kabra1110 commented 1 year ago

First we have to implement dict.keys and dict.values (1 PR)

Should dict.keys be a list? CPython uses view objects, which do not support many list operations.

czgdp1807 commented 1 year ago

That’s true. For now let’s keep it as a list. Later if needed we can create a separate type for dict.keys.