Python List(List)

Sequences are the most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index, the first index is 0, the second index is 1, and so on.

Python has built-in types of 6 sequences, but the most common are lists and tuples.

The sequence can be done by indexing, slicing, adding, multiplying, and checking members.

In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.


list is the most commonly used Python data type, and it can appear as a comma-separated value in square brackets.

List of data items does not need to have the same type

Create a list by enclosing the different data items separated by commas in square brackets. As shown below:

list1 = ['physics' , 'chemistry', 1997 , 2000] list2 = [1, 2, 3, 4 , 5 ] list3= ["a", "b" , "c", "d"]

Like the index of a string, the list index starts at 0. The list can be intercepted, combined, and so on.

Access the value in the list

Use the subscript index to access the values in the list. You can also use the square brackets to intercept characters as follows:

Instance (Python 2.0+)

#!/usr/bin/python list1 = ['physics', 'chemistry' , 1997, 2000] list2 = [1, 2, 3, 4 , 5, 6, 7 ] print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]

The output of the above example:

list1[0]: physics
List2[1:5]: [2, 3, 4, 5]

Update list

You can modify or update the data items in the list. You can also use the append() method to add list items as follows:

Instance (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- list= [] ## empty list list.append('Google') ## Add elements using append() list.append('WELOOKUPS') print list

Note: We will discuss the use of the append() method in the next section

The output of the above example:

['Google',  'WELOOKUPS']

Delete list elements

You can use the del statement to remove elements of a list, as in the following example:

Instance (Python 2.0+)

#!/usr/bin/python list1 = ['physics', 'chemistry' , 1997, 2000] print list1 del list1[2] print "After deleting value at index 2 : " print list1

The above example output:

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

note:We will discuss the use of the remove() method in the next section.

Python list script operator

List operators for + and * are similar to strings. The + sign is used for the combined list and the * is used for the repeat list.

As shown below:

Python ExpressionResults Description
len([1, 2, 3])3length
[1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6] combination< /td>
['Hi!'] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']Repeat
3 in [1, 2, 3]TrueIs the element present in the list
for x in [1, 2, 3]: print x,1 2 3iteration

Python list interception

Python's list interception example is as follows:

>>>L = ['Google', 'WELOOKUPS', 'Python'] >>> L[2] 'Python' >>> L[-2] 'WELOOKUPS' >>> L[1:] ['WELOOKUPS', 'Python'] >>>

welookups is optimized for learning.© welookups 2018 -
All Right Reserved and you agree to have read and accepted our term and condition