Python List functions


In this tutorial, we’re gonna look at many functions to work with Python list: create, combine, add, remove, search, sort, convert, copy…

Related Posts:
How to iterate over a List in Python
Python Sort Class Object List – with Python sorted()
Python Sort List of Tuples – using Python sorted() built-in
Python Dictionary – Data Structure

Declare a Python List

A Python list begins with an opening square bracket and ends with a closing square bracket, []. Items in the List are separated with commas:

Get an item in a Python List by index

Simple list – one dimensional list

The first value in the list is at index 0, the second value is at index 1, the third value is at index 2

For example:

Lists inside Python List using multiple indexes

Python List Negative index

we can also use negative integers for indexes. -1 refers to the last index, -2 for the second-to-last index…

For example:

Get a Sublist of a Python List

We can get several values from a list, in the form of a new list using square brackets with two integers separated by a colon:
– the first integer is the starting index
– the second integer is the ending index (but the list will not include)

We can leave out one or both of the indexes inside square brackets:
– the first index is the same as using 0 (beginning of the list)
– the second index is the same as using the length of the list

Get length of a Python List

len() function will return the number of items in a list:

Change item value in a Python List with index

Combine Python Lists

+ operator can combine lists to create a new list:

Replicate a Python List

* operator can be used with an integer to replicate the list:

Add items to a Python List

Use + Operator

We use list = list + [new_item/list_items] to append one new_item or a list_items to list:

Use append() method

With append(item), we can add only one item to the end of the list for each time:

If we try to use append() method to add an item list, the result will be appended a list, not items inside the list:

Use insert() method

We use insert(index, item) method to insert an item at any index in the list.

If the index is larger than length of the list, item will be appended to the end of the list.

Remove item from Python List

Use del statement

del statement will delete item at an index in a list. All items after the deleted item will be moved up one index.

Use remove() method

We use remove(item) to delete item from a list:

Check if an item value exists in a Python List

We can know whether a value is or is not in a list using in and not in operators:

Python multiple assignment

We can assign multiple variables with the items in a list in just one line of code:

Search an item in a Python List

We can use index() method with item value as parameter to search an item. If that
value exists in the list, the method will return the index:

Sort items in a Python List

We can easily sort lists of number values or lists of strings using sort() method:

We can also pass reverse=True argument to have sort() sort the values in reverse order:

Convert a Python List to Tuple

Use tuple(list) function:

Convert a Tuple to Python List

Use list(tuple) function:

Python List references

When we assign a list to a variable, we are actually assigning a list reference to the variable:

Copy a Python List

Python provides a copy module that provides both copy() and deepcopy() functions.
copy.copy(): make a duplicate copy of a list
copy.deepcopy(): copy also inner lists

For example:

By grokonez | December 19, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *