DEV Community

Cover image for Unlock the Power of Arrays: Tips and Tricks for Working with Arrays
David Mebo
David Mebo

Posted on

Unlock the Power of Arrays: Tips and Tricks for Working with Arrays

It is no news that most wizards in the programming community, even accomplished polyglots do not like Python due to its weird(?) way of doing things. Still, hey, technology is all about innovation and change.

Some things to note though when working with arrays in Python:

  1. They are called lists.
  2. There are other types of arrays that are similar, but different from lists, like tuples, dictionaries,...
  3. Know the syntax for instantiating a list, e.g., [1, 2, 3, 4], [1] + [2] * [3].
  4. Know how to use the functions insert, append, and remove, and do not be afraid to use them if you cannot do the operations manually. It's all about innovation and using the libraries and functions already provided instead of trying to reinvent the wheel, unless you are asked not to (looking at you, C).
  5. Know how to instantiate a 2D array, e.g., [[1, 2, 3], [4, 5, 6], [7, 8]].
  6. Checking if a value is present in an array is as simple as i in arr.
  7. Understand how copy and deep copy work, the difference between the two, and when to use them. E.g., A = B and A = list(B), where B(list) is a different copy of B (more on that in the future).
  8. Slice is your best bud, use it wisely.

There may be some unmentioned here, but if you know any, I appreciate it if you could share.

Top comments (0)