Python List tutorial

In this tutorial, we are going to see about Python List.

Python List is one of the most used data structure in Python. Python List is a collection of items in ordered sequence.There are 6 sequences in python and List is one of them.

Python list begins with an opening square bracket and ends with a closing square bracket, [] and you can put multiple data types into a list.

Let’s learn list with a lot of examples.

Create a list

You can create a list simply using square brackets.

Output:

List 1: [1, 2, 3, 4] List 2: [1, 2, 3, 4]

Access list elements

You can access list elements with square brackets.Let’s see with the help of example.

Output:

First fruit: Apple
Second fruit: Orange
Third fruit: Banana
Fourth fruit: Pineapple

Slicing list

You can use slicing as similar to String in the list.
Use following syntax for slicing.
list1[start:end:step] All the variables start, end and step are optional.
Start: It denotes starting of the list
Start: It denotes ending of the list
step: It denotes steps for accessing element.
Let’s understand with the help of example.

Output:

First two elements: [‘Apple’, ‘Orange’] Last two elements: [‘Banana’, ‘Pineapple’] Middle two elements: [‘Orange’, ‘Banana’] Elements in step 2 [‘Apple’, ‘Banana’]

Please go through above examples and you will be able understand how slicing works.Let’s
You can provide negative indexes also to traverse list in reverse order. -1 is the last index in the list.
Let’s understand with the help of example.

Output:

last three elements: [‘Orange’, ‘Banana’, ‘Pineapple’] Print list in reverse order: [‘Pineapple’, ‘Banana’, ‘Orange’, ‘Apple’]

Python list methods

Please find list of methods which you can use with Python Lists.

MethodDescription
appendThis method is used to add single item to the end of the list.
extendThis method is used to add list of items to the end of the list
removeThis method is used to remove element from the list.If element is not present then it will raise error
indexThis method is used to find index of given element
popThis method will remove and return passed index.If index is not passed then it will remove and return last element from the list.
countThis method will count occurences of passed element in the list
clearThis method is used to remove all elements of the list.
copyThis method is used to shallow copy the list.
sortThis method is used to sort the list in ascending or desending order
reverseThis method is used to reverse the list.
insertThis method is used to add element to the list at specified index.

Get List’s length

You can use Python’s len function to get length of the list.

Output:

Length of listOfFruits is: 4

Changing Values in a List with Indexes

You can change value at any index using = operator.
Let’s understand with the help of simple example.

Output:

listOfFruits before: [‘Apple’, ‘Orange’, ‘Banana’, ‘Pineapple’] listOfFruits after: [‘Apple’, ‘Mango’, ‘Banana’, ‘Pineapple’]

As you can see, value at index 1 of the list changed from ‘Orange’ to ‘Mango’.

List Concatenation and List Replication

You can use + operator to concatenate lists.Please note that it won’t impact original list.If you want to make changes to first list, you need to explicitly assign it.

Let’s understand with the help of simple example.

Output:

ListOfFruits: [‘Apple’, ‘Orange’, ‘Banana’, ‘Pineapple’]

You can replicate list with the help of * operator

Output:

List 1: [‘Apple’, ‘Orange’, ‘Apple’, ‘Orange’]

That’s all about Python List.


import_contacts

You may also like:

Related Posts

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusion In this post, we […]

  • Python List of dictionaries
    09 December

    List of Dictionaries in Python

    Table of ContentsHow to Create a List of Dictionaries in Python?How to Access Elements of a List of Dictionaries?Search in a List of Dictionaries in PythonUpdate Values in a List of Dictionaries in PythonHow to Sort List of Dictionaries in Python?Conclusion We use both lists and dictionaries in our python programs. Lists are used to […]

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

  • Remove nan from list in Python
    16 November

    Remove nan From List In Python?

    Table of ContentsWhat is nan in python?How to remove nan from list in python?Remove nan from list using the str() functionRemove nan from list using math.isnan() functionRemove nan from list using numPy.isnan() functionRemove nan from list using pandas.isnull() functionConclusion In python, nan is a special value used to represent missing values. In this article, we […]

  • List intersection in Python
    11 November

    List intersection in python

    Table of ContentsWhat is the list intersection in Python?Ways to find the list intersection in PythonUsing the for loop to achieve list intersection in PythonUsing the set() function to find list intersection in PythonUsing the intersection() function to find list intersection in PythonUsing the filter() and lambda functions to find list intersection in PythonUsing the […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.