Python List

In Python, the sequence of various data types is stored in a list. A list is a collection of different kinds of values or items. Since Python lists are mutable, we can change their elements after forming. The comma (,) and the square brackets [enclose the List's items] serve as separators.

Although six Python data types can hold sequences, the List is the most common and reliable form. A list, a type of sequence data, is used to store the collection of data. Tuples and Strings are two similar data formats for sequences.

Lists written in Python are identical to dynamically scaled arrays defined in other languages, such as Array List in Java and Vector in C++. A list is a collection of items separated by commas and denoted by the symbol [].

List Declaration

Code

Output:

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

Characteristics of Lists

The characteristics of the List are as follows:

  • The lists are in order.
  • The list element can be accessed via the index.
  • The mutable type of List is
  • The rundowns are changeable sorts.
  • The number of various elements can be stored in a list.

Ordered List Checking

Code

Output:

False

The indistinguishable components were remembered for the two records; however, the subsequent rundown changed the file position of the fifth component, which is against the rundowns' planned request. False is returned when the two lists are compared.

Code

Output:

True

Records forever protect the component's structure. Because of this, it is an arranged collection of things.

Let's take a closer look at the list example.

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 preceding illustration, we printed the employee and department-specific details from lists that we had created. To better comprehend the List's concept, look at the code above.

List Indexing and Splitting

The indexing procedure is carried out similarly to string processing. The slice operator [] can be used to get to the List's components.

The index ranges from 0 to length -1. The 0th index is where the List's first element is stored; the 1st index is where the second element is stored, and so on.

Python List

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

  • The beginning indicates the beginning record position of the rundown.
  • The stop signifies the last record position of the rundown.
  • Within a start, the step is used to skip the nth element: stop.

The start parameter is the initial index, the step is the ending index, and the value of the end parameter is the number of elements that are "stepped" through. The default value for the step is one without a specific value. Inside the resultant Sub List, the same with record start would be available, yet the one with the file finish will not. The first element in a list appears to have an index of zero.

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 programming languages, Python lets you use negative indexing as well. The negative indices are counted from the right. The index -1 represents the final element on the List's right side, followed by the index -2 for the next member on the left, 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]

Negative indexing allows us to obtain an element, as previously mentioned. The rightmost item in the List was returned by the first print statement in the code above. The second print statement returned the sub-list, and so on.

Updating List Values

Due to their mutability and the slice and assignment operator's ability to update their values, lists are Python's most adaptable data structure. Python's append() and insert() methods can also add values to a 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 redundancy administrator empowers the rundown components to be rehashed on different occasions.

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

The append() function in Python can add a new item to the List. In any case, the annex() capability can enhance the finish of the rundown.

Consider the accompanying model, where we take the components of the rundown from the client and print the rundown on the control center.

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

The remove() function in Python can remove an element from the List. To comprehend this idea, look at the example that follows.

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- Create a program to eliminate the List's duplicate items.

Code

Output:

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

Example:2- Compose a program to track down the amount of the component in the rundown.

Code

Output:

The sum is: 67
In [8]:

Example: 3- Compose the program to find the rundowns comprise of somewhere around one normal component.

Code

Output:

The common element is: 2

Next TopicPython Tuples