How does one run multiple expressions in one line in python?

I have:


but I’d like it to be

            arch_indices.append(a_idx); arch_hp_indices.append(a_hp_idx); arch.append(a) ;arch_hp.append(a_hp);;

Like I would do in OCAML. How can I execute multiple commands in one like in python?

You can just use semicolons. But why would you want to? It’s less readable and adds no value that I can see. Python is about readability. If you try to cram things onto one line, you’ll very soon find places where you simply can’t do what you want (e.g., loops cannot be put on one line, in general).

Better to learn to use the language idiomatically than to try to force it to work like something else just because it’s familiar to you.

1 Like

I dont know how to learn the language idiomatically. How do I do that?

There’s plenty of tutorials available online - or if you’re an experienced programmer in other languages, reading other people’s Python code might suit you better and give you a feel for what idiomatic Python looks like.

In addition to what @pf_moore said regarding looking through tutorials and the code of other people, I would highly recommend checking out PEP-8. It’s quite lengthy, but it’s the official styling guide for Python and well worth the read. This won’t tell you everything you need to know, but it’s a great place to start.

Also, regarding the specific code example you’re using, instead of using a separate list for the indexes, you can simply use a tuple. Typically the first item is the index, and the second one is the actual value:

            arch.append((a_idx, a))
            arch_hp.append(a_hp_idx, a_hp)

A significant part of idiomatic code design also involves utilizing the correct data structures and class design. Much of this is covered in the official tutorial.