Seeking Guidance and Best Practices for Lists and Tuples in Mojo Language
Hello Wonderful Mojo Community!
I'm excited to embark on my journey with the Mojo language and am particularly interested in developing datetime and strings libraries. As a newcomer, I'm eager to learn and contribute to this vibrant community.
However, I've encountered a bit of a hurdle while working with lists and tuples. Unlike Python, where I can easily access items in a list or tuple, in Mojo, I receive an error stating:
it does not implement the '__getitem__'/'__setitem__' or '__refitem__' methods.
I'm keen to understand the best practices for handling lists and tuples in Mojo. Could anyone provide guidance or share their experiences on how to navigate this? I'm looking forward to your insights and tips to enhance my understanding and skills in Mojo.
Thank you all in advance for your support and assistance!25 Replies
Congrats @Aamir, you just advanced to level 1!
you're probably used to using square brackets on python's lists, for mojo's ListLiteral you'll need to use the .get method https://docs.modular.com/mojo/stdlib/builtin/builtin_list.html#get
or alternatively, you might be able to just use a dynamicvector if your items in the list are all the same type
Yeah, I had tried that. However, I dont know the right way to invoke
get
function. It didn't work for me!Is there any book or guided language tour that I can try?
Also, when I tried to import DynamicVector, I get this error!
I am using
mojo 0.7.0 (af002202)
on windows Ubuntu WSL.I need to pass, get value at the given
'i
index.About Vector import. I followed this!
After importing the DynamicVector from collections. That error went away, but it started crashing again!
Compiler is crashing now!
Is this language stable? I am not able to move an inch. There should be proper guided language tour with documentations. The way Go did.
it's fairly common to have the parser crashing, remember that this language is pre 1.0, you are helping with early testing
I see!
I understand, no worries
while it may take some fiddling to get the dynamicvector working, you're on the right path to getting the program working if you need to index into a collection with a value from a variable
But, after adding dynamic vector, I can't move ahead, due to parser crash. How do you guyw deal with such issues.
personally, delete things until it's back at a state where the ide help works again, i'm only a couple days into trying it myself
Removing this line,
from collections.Vector import DynamicVector
solves the issue. But that is important one. I need to use list or tuple and want to fetch values with dynamic variables instead of constantsyou can try may not even need to be imported, might just be in prelude is just in the prelude already, no need to import
mojo filename.mojo
to attempt running it and get a slightly more specific message (and stack dump)
in this specific case though, i believe vector is just lowercase, collections.vector
DynamicVector I get this!
Congrats @Aamir, you just advanced to level 2!
if you go through it slowly top to bottom, you'll see
unable to locate module 'Vector'
- in the docs it's lowercaseI see, but in the documentation it is in Capital case!
make sure you use the latest from https://docs.modular.com/mojo/ - third party sites may be weeks or more out of date
Modular Docs - Mojo🔥
A programming language that bridges the gap between AI research and production, unlocking speed and usability.
👍🏼
we're both gonna make it