Javatpoint Logo
Javatpoint Logo

Python List

A list in Python is used to store the sequence of various types of data. A list can be defined as a collection of values or items of different types. Python lists are mutable type which implies that we may modify its element after it has been formed. The items in the list are separated with the comma (,) and enclosed with the square brackets [].

Although Python has six data types that may hold sequences, the list is the most popular and dependable form. The collection of data is stored in a list, a sequence data type. Similar sequence data formats are Tuples and String.

Python lists are identical to dynamically scaled arrays that are specified in other languages, such as Java's ArrayList and C++'s vector. A list is a group of items that are denoted by the symbol [] and subdivided by commas.

List Declaration

Code

Output:

[1, 2, 'Python', 'Program', 15.9]
['Amy', 'Ryan', 'Henry', 'Emma']
< class ' list ' >
< class ' list ' >

Characteristics of Lists

The list has the following characteristics:

  • The lists are ordered.
  • The element of the list can access by index.
  • The lists are the mutable type.
  • The lists are mutable types.
  • A list can store the number of various elements.

Ordered List Checking

Code

Output:

False

The identical elements were included in both lists, but the second list modified the index position of the fifth element, which is against the lists' intended order. When the two lists are compared, false is returned.

Code

Output:

True

Lists permanently preserve the element's order. It is the arranged gathering of things because of this.

Let's have a look at the list example in detail.

Code

Output:

printing employee data...
Name : John, ID: 102, Country: USA
printing departments...
Department 1:
Name: CS, ID: 11
Department 2:
Name: IT, ID: 11
HOD Details ....
CS HOD Name: Mr. Holding, Id: 10
IT HOD Name: Mr. Bewon, Id: 11
<class ' list '> <class ' list '> <class ' list '> <class ' list '> <class ' list '>

In the above example, we have created the lists which consist of the employee and department details and printed the corresponding details. Observe the above code to understand the concept of the list better.

List Indexing and Splitting

The indexing is processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator [].

The index starts from 0 and goes to length - 1. The first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on.

Python List

We can get the sub-list of the list using the following syntax.

  • The start denotes the starting index position of the list.
  • The stop denotes the last index position of the list.
  • The step is used to skip the nth element within a start:stop

The initial index is represented by the start parameter, the ending index is determined by the step, and also the number of elements which are "stepped" through is the value of the end parameter. In the absence of a specific value for step, the default value equals 1. Inside the resultant SubList, the item also with index start would be present, but the one with the index finish will not. A list's initial element seems to have the index of 0.

Consider the following example:

Code

Output:

1
2
3
4
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[3, 4, 5]
[2, 4, 6]

In contrast to other languages, Python gives you the option to employ negative indexing as well. From the right, the negative indices are counted. The final element on the right-hand side of the list is represented by the index -1, followed by the next member on the left at the index -2, and so on until the last element on the left is reached.

Python List

Let's have a look at the following example where we will use negative indexing to access the elements of the list.

Code

Output:

5
[3, 4, 5]
[1, 2, 3, 4]
[3, 4]

As we discussed above, we can get an element by using negative indexing. In the above code, the first print statement returned the rightmost element of the list. The second print statement returned the sub-list, and so on.

Updating List Values

Lists are the most versatile data structures in Python since they are mutable, and their values can be updated by using the slice and assignment operator. Python also provides append() and insert() methods, which can be used to add values to the list.

Consider the following example to update the values inside the list.

Code

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]

The list elements can also be deleted by using the del keyword. Python also provides us the remove() method if we do not know which element is to be deleted from the list.

Consider the following example to delete the list elements.

Code

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]

Python List Operations

The concatenation (+) and repetition (*) operators work in the same way as they were working with the strings. The different operations of list are

  1. Repetition
  2. Concatenation
  3. Length
  4. Iteration
  5. Membership

Let's see how the list responds to various operators.

1. Repetition

The repetition operator enables the list elements to be repeated multiple times.

Code

Output:

[12, 14, 16, 18, 20, 12, 14, 16, 18, 20]

2. Concatenation

It concatenates the list mentioned on either side of the operator.

Code

Output:

[12, 14, 16, 18, 20, 9, 10, 32, 54, 86]

3. Length

It is used to get the length of the list

Code

Output:

9

4. Iteration

The for loop is used to iterate over the list elements.

Code

Output:

12
14
16
39
40

5. Membership

It returns true if a particular item exists in a particular list otherwise false.

Code

Output:

False
False
False
True
True
True

Iterating a List

A list can be iterated by using a for - in loop. A simple list containing four strings, which can be iterated as follows.

Code

Output:

John
David
James
Jonathan

Adding Elements to the List

Python provides append() function which is used to add an element to the list. However, the append() function can only add value to the end of the list.

Consider the following example in which, we are taking the elements of the list from the user and printing the list on the console.

Code

Output:

Enter the number of elements in the list:10
Enter the item:32
Enter the item:56
Enter the item:81
Enter the item:2
Enter the item:34
Enter the item:65
Enter the item:09
Enter the item:66
Enter the item:12
Enter the item:18
printing the list items..
32  56  81  2  34  65  09  66  12  18 

Removing Elements from the List

Python provides the remove() function which is used to remove the element from the list. Consider the following example to understand this concept.

Example -

Code

Output:

printing original list: 
0 1 2 3 4 
printing the list after the removal of first element...
0 1 3 4 

Python List Built-in Functions

Python provides the following built-in functions, which can be used with the lists.

  1. len()
  2. max()
  3. min()

len( )

It is used to calculate the length of the list.

Code

Output:

6

Max( )

It returns the maximum element of the list

Code

Output:

782

Min( )

It returns the minimum element of the list

Code

Output:

103

Let's have a look at the few list examples.

Example: 1- Write the program to remove the duplicate element of the list.

Code

Output:

[1, 2, 3, 55, 98, 65, 13, 29]

Example:2- Write a program to find the sum of the element in the list.

Code

Output:

The sum is: 67
In [8]:

Example: 3- Write the program to find the lists consist of at least one common element.

Code

Output:

The common element is: 2

Next TopicPython Tuples





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA