Python

11 Great List Methods That Most Don’t Know

What will you learn?

After this list you will know the most useful methods and techniques with Python lists. It is funny, that many of these are unknown to many that work with Python. Be sure not to be one of them as they can save you a lot of time.

#1 in

This one is probably my favorite. To check if an element is in the list. This is easy with Python and just why we can’t stop loving it.

this_list = ['foo', 'bar', 'foobar', 'barfoo']

if 'foo' in this_list:
    print('yes')

if 'foofoo' in this_list:
    print('no')

This will only print yes as ‘foofoo‘ is not an element in the list this_list.

On the other hand ‘foo‘ is in the list, hence it prints yes.

#2 sorted()

This is actually a built-in function and it returns a new sorted list from the items in an iterable.

An iterable is a list or anything you can iterate over in Python.

Let’s try it.

this_list = ['foo', 'bar', 'foobar', 'barfoo']

print(sorted(this_list))
print(sorted(this_list, reverse=True))

This will output.

['bar', 'barfoo', 'foo', 'foobar']
['foobar', 'foo', 'barfoo', 'bar']

Hence, it can also sort in reverse order.

It is important to understand that it returns a new list with the items from the input argument.

Want to learn more Built-in functions?

#3 sort()

This is kind of funny. And it might look like the same. But there is a big difference.

The method sort() will sort the list in-place and not return a new list.

Let’s try it.

this_list = ['foo', 'bar', 'foobar', 'barfoo']

this_list.sort()
print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar']

What was the difference? Well, sorted() returns a new list, and sort() sorts the list and changes the order of the element of the original list.

Why does that matter?

There can be many reasons – but one major one is if the list is huge and you need to save space and time – then in-place sorting is both more memory and speed efficient.

#4 for

Again, a favorite of mine. Iterating over a list in Python is such a pleasure. Why make complex syntax for something you need all the time?

Well, in Python it is easy to do.

this_list = ['bar', 'barfoo', 'foo', 'foobar']

for item in this_list:
    print(item)

This will output.

bar
barfoo
foo
foobar

#5 append()

Adding an element to the end of a list is something you need all the time. Again, Python does it in a simple manner.

this_list = ['bar', 'barfoo', 'foo', 'foobar']

this_list.append('foobarfoo')

print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

And see the added (appended) element at the end of the list.

#6 concatenate lists

This one is great. Again Python goes the extra mile to make things a simple as possible.

If you need to concatenate two lists together. How is that done?

Well, with the addition sign. See this.

list_a = ['foo', 'bar']
list_b = ['foobar', 'barfoo']

list_c = list_a + list_b

print(list_c)

This will output.

['foo', 'bar', 'foobar', 'barfoo']

#7 index()

Sometimes you need the index of an element in a list.

Well, here you go.

this_list = ['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

print(this_list.index('foo'))

This will output 2, as ‘foo‘ has the first occurrence in the list at index 2 (remember, that Python lists are zero indexed – meaning that the first element is 0, second element is 1, etc.).

#8 copy()

Sometimes you need a new copy of a list. Let’s try and understand what that means.

this_list = ['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

list_copy = this_list.copy()

list_copy.append('me element')

print(list_copy)
print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'me element']
['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

As you see, the append only modifies the list it is appended to. This might not surprise you.

But let’s try something different.

a = this_list

a.append('element')

print(a)
print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'element']
['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'element']

Oh no – the element got appended to both lists – or what?

No, actually it is the same list, it is just two variables, which point to the same list.

Now copy starts to make sense.

#9 remove()

Now what if you need to remove an element form this list?

Let’s try how easy it could be.

this_list = ['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'element']

this_list.remove('element')

print(this_list)

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

Oh, it was that simple.

#10 pop()

Actually a Python list can be used as a Stack out of the box.

Append (as we saw above) pushes an element to the back of the list – or you could say, the top of the stack.

And then, what does pop do?

this_list = ['barfoo', 'foo', 'foobar', 'foobarfoo']

element = this_list.pop()

print(element)
print(this_list)

This will output.

foobarfoo
['barfoo', 'foo', 'foobar']

As you see, this is just as a stack.

You might complain, it does not have the same performance (big O) as a real stack. Well, because the list is implemented in such an awesome way, it has asymptotic the the same big-O complexity. What does that mean? That it is just as good as a “real” stack.

#11 del

This is actually one I don’t use that much – but it is handy if you like this notation.

Let’s just try it.

this_list = ['barfoo', 'foo', 'foobar']

del this_list[1]

print(this_list)

This will delete element at position 1 and give this output.

['barfoo', 'foobar']

Wow. That was great.

Want to learn more?

If this is something you like and you want to get started with Python, then check my 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.’
Rune

Share
Published by
Rune

Recent Posts

Learn Python FREE Online

Why learn Python? There are many reasons to learn Python, and that is the power…

3 days ago

How to Check if a Number is Even or Odd with Python

What will you learn? How to use the modulo operator to check if a number…

1 week ago

The Truth About Being a Python Software Contractor

There are a lot of Myths out there There are lot of Myths about being…

2 months ago

Do This and 10X Your Salary as a Software Engineer

To be honest, I am not really a great programmer - that is not what…

2 months ago

Ultimate Guide to the Data Science Career Path

What does it take to become a Data Scientist? Data Science is in a cross…

2 months ago

How to Setup a MySQL Server in Docker for Your Python Project

What will you learn? Need to setup a SQL server? You don’t need to install…

4 months ago