Explain me this: Lists in Python + Cheatsheet
Welcome to Another one of my Explains, today we’ll be checking out Python lists in the form of questions and answers and at the end, we’ll have a free cheat sheet for you. So without further ado, let’s get started!
- What are lists and why should I care?
Well, they are one of the principal data structures in python alongside dict, set and tuple and you should care as they are also the most versatile data types in python that allow us to work with multiple elements at once.
2. Alright I’m sort of interested, so where to start?
Well, let’s talk about the creation of a list, well thousands of years ago .. never mind, look simply put a list is written by enumerating its elements between square brackets [ and ], with the elements separated by commas.
eg. friends = [“first”, “second”, “third”, “fourth”, “fifth”]
3. Seems easy enough, so [] and commas, next?
Now that you know how to put stuff on the list, let’s talk about taking stuff out of it. So accessing elements from a list could be done in 2 ways, either through index or slicing.
4. Okay, what’s an index and how can I use it?
So Refer to the below figure, The indices of a list start with 0. The last element can also be accessed with the index −1, the second last with −2, and so on.
In short, positive index — forward, negative index — backward
5. Then what’s slicing useful for?
Well, We can access a range of items in a list by using the slicing operator :
, here’s a shortlist of the uses
- L[i] the ith element of L
- L[i:j] the list of elements with indices starting at i and up to (but not including) j
- L[:j] the first j elements
- L[i:] all the elements from the ith onwards
- L[-i:] the last i elements of L
- L[i:j:k] elements from the ith up to (but not including) the jth, taking only every kth element
- L[::2] the elements of L with even indices
- L[::-1] a reverse copy of L
6. Damn this is confusing, do you have simple examples?
Of course, here you go
6. Got it, so “:” used in a variety of ways, I know how to create and access a list in the 2 ways, Now what ?
Now we add/modify stuff in it. See, Lists are mutable, meaning their elements can be changed .. ahem unlike others( Yes I’m looking at you string and tuple )
Basically, we can do that in 4 ways
- We can use the assignment operator
=
to change an item or a range of items - We can add one item to a list using the
append()
method or add several items using theextend()
method. - We can also use
+
operator to combine two lists. A fancy word for this, concatenation. P.S : The*
the operator repeats a list for the given number of times - Furthermore, we can insert one item at a desired location by using the method
insert()
or insert multiple items by squeezing it into an empty slice of a list.
Can’t still visualize all this can you ;)? No worries, here you go with examples
7. Alright so creating a list, accessing , adding / modifying stuff , now what deleting stuff ?
My my, are you catching on. Yup deleting it is.
3 ways : pop, remove and clear
We can use remove()
it to remove the given item or pop()
to remove an item at the given index.
The pop()
the method removes and returns the last item if the index is not provided. This helps us implement lists as stacks (first in, last out data structure)
And, if we have to empty the whole list, we can use the clear()
method.
8. Alright any plans of ending soon, its been quite a while, what’s left ?
Almost Done, Now, we come to list methods, Python’s a beautiful language and they have some inbuilt methods for lists. For the pros, I’ve also given you the complexity of them in terms of n, the length of the list L.
- len(L) returns the number of elements of the list L O(1)
- sorted(L) returns a sorted copy of the list L O(n log n)
- L.sort() sorts L in place O(n log n)
- L.count(c) the number of occurrences of c in L O(n)
- c in L is the element c found in L? O(n)
- L.append(c) append c to the end of L amortised O(1)
- L.pop() extracts and returns the last element of L amortised O(1)
I know, I know, example time —
9. That’s it ?
No Actually, far from it. We have stuff like list comprehension, and a plethora of other list methods, but this is a great start. Please read the official python docs, its super extensive and now you’ll understand exactly whats happening.
10. Wait what about that cheatsheet?
Oh yes, I almost forgot. Here’s a github link. Stay starred as all DS files will be coming to this project within a month.
So that's it, from my side, all the kidding apart, I would really love some feedback about the article. Furthermore, detailed articles for every DS will be dropping, if interested please check those as well.
Have a great day, Cheers!