List Manipulation Python Tutorial



This Python 3 programming tutorial covers list manipulation. This includes adding things to the end, inserting them into specific positions, removing things, finding data, counting the number of occurrences, sorting, and reversing the data.

All of the above are very common operations with lists, and all of them are built into Python 3 for ease of use.

Keep in mind that lists are mutable, and using these functions change the list.

Here is some example code of list manipulation:

Since lists are mutable, this means that we will be using lists for things where we might intend to manipulate the list of data, so how can we do that? Turns out we can do all sorts of things.

We can add, remove, count, sort, search, and do quite a few other things to python lists.

# first we need an example list:
x = [1,6,3,2,6,1,2,6,7]
# lets add something.
# we can do .append, which will add something to the end of the list, like:
x.append(55)
print(x)

Above, we took a list, and added to the end of the list with .append. Remember append with files? Same thing, .append() will add to the end of a list.

What if you have an exact place that you'd like to put something in a list, instead of just at the very end?

x.insert(2,33)
print(x)

Here we say that we want to insert, at the index of 2, the number 33. The reason that went in the 3rd place, again, is because we start at the zero element, then go 1, 2...etc with lists.

Now we can remove things. .remove() will remove the first instance of the value in the list. If it doesn't exist, there will be an error:

x.remove(6)
print(x)

Next, remember how we can reference an item by index in a list? like:

print(x[5])

Well, we can also search for this index, like so:

print(x.index(1))

Now here, we can see that it actually returned a 0, meaning the first element was a 1... when we knew there was another with an index of 5. So, instead we might want to know before-hand how many examples there are.

print(x.count(1))

We see there are actually 2 of them

We can also sort the list:

x.sort()
print(x)

What if these were strings? like:

y = ['Jan','Dan','Bob','Alice','Jon','Jack']
y.sort()
print(y)
y.reverse()
print(y)

The next tutorial:





  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial