List in Python | How to use list in python | Python Lists

List in Python

list in python


Lists are similar to arrays in C. However; the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets []. 

We can use slice [:] operators, to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.

Example:

list = [ 'abc'123 , 'hi'10.12 , 10 ]
print (list)
print (list[0])
print (list[2:4])
print (list[2:])
print (list * 2)
print (list + list)

Output:

['abc'123'hi'10.1210]
abc
['hi'10.12]
['hi'10.1210]
['abc'123'hi'10.1210'abc'123'hi'10.1210]
['abc'123'hi'10.1210'abc'123'hi'10.1210]


Access Python List 

Python allows us to access values from the list in various ways.

Example:

list = [ 'abc'123 , 'hi'10.12 , 10 ]
print (list[0])
print (list[2:4])
print (list[2:])
print (list[-3:-1])

Output:

abc
['hi'10.12]
['hi'10.1210]
['hi'10.12]


Adding Python Lists 

In Python, lists can be added by using the concatenation operator(+) to join two lists.

Example:

list = [ 'abc'123]
list1 = [ 'hi'10.12 , 10 ]
print (list+list1) 

Output:

['abc'123'hi'10.1210]


Replicating lists 

Replicating means repeating, It can be performed by using '*' operator by a specific number of times.

Example: 

list = [ 'abc'123]
print (list*2

Output:

['abc'123'abc'123]


List Slicing 

A subpart of a list can be retrieved on the basis of an index. This subpart is known as list slice. This feature allows us to get sub-list of specified start and end index.

Example:

list = [ 'abc'123 , 'hi'10.12 , 10 ]
print (list[0])
print (list[2:4])
print (list[2:])

Output:

abc
['hi'10.12]
['hi'10.1210]


Updating List 

To update or change the value of a particular index of a list, assign the value to that particular index of the List.

Example:

list = [ 'abc'123 , 'hi'10.12 , 10 ]
print (list)
list[2] = 'how'
print (list)

Output:

['abc'123'hi'10.1210]
['abc'123'how'10.1210]


Appending Python 

List Python provides, append() method which is used to append i.e., add an element at the end of the existing elements.

Example:

list = [ 'abc'123 , 'hi'10.12 , 10 ]
list.append('how')
print (list)

Output:

['abc'123'hi'10.1210'how']


Deleting Elements 

In Python, a del statement can be used to delete an element from the list. It can also be used to delete all items from startIndex to endIndex.

Example:

list = [ 'abc'123 , 'hi'10.12 , 10 ]
del list[2:4]
print (list)

output:

['abc'12310]

Following are the common list functions.

min() method 

This method is used to get min value from the list.

Example:

list = [ 1123 , 10210.12 , 10 ]
print (min(list))

Output:

1


max() method 

This method is used to get the max value from the list.

Example:

list = [ 1123 , 10210.12 , 10 ]
print (max(list))

Output:

123


len() method 

This method is used to get the length of the list.

Example:

list = [ 1123 , 10210.12 , 10 ]
print (len(list))

Output:

5


Following are the built-in methods of List

index(object) Method 

It returns the index value of the object.

Example:

list = [ 1123 , 10210.12 , 10]
print (list.index(102))

Output:

2


count(object) Method 

It returns the number of times an object is repeated in the list.

Example:

list = [ 1123 , 10210.12 , 10 ] 
print (list.count(102))
list1 = [ 1123 , 102102 , 10 ]
print (list1.count(102))

Output:

1
2


pop()/pop(index) Method 

It returns the last object or the specified indexed object. It removes the popped object.

Example:

list = [ 1123 , 10210.12 , 10 ]
print (list.pop())
print (list.pop(2))
print (list)

Output:

10
102
[112310.12]


insert(index,object) Method 

It inserts an object at the given index.

Example:

list = [ 1123 , 10210.12 , 10 ]
print (list.insert(3,20))
print (list)

Output:

[11231022010.1210]


extend(sequence) Method 

It adds the sequence to the existing list. 

Example:

list = [ 1123 , 10210.12 , 10 ]
list1 = [ 3 , 4 ]
print (list.append(list1))
print (list)

Output:

[112310210.1210, [34]]


remove(object) Method 

It removes the object from the given list. 

Example:

list = [ 1123 , 10210.12 , 10 ]
list.remove(123)
print (list)

Output:

[110210.1210]


reverse()Method 

It reverses the position of all the elements of a list.

Example:

list = [ 1123 , 10210.12 , 10 ]
list.reverse()
print (list)

Output:

[1010.121021231]


sort() Method 

It is used to sort the elements of the List. 

Example:

list = [ 1123 , 10210.12 , 10 ]
list.sort()
print (list)

Output:

[11010.12102123]
Next Post Previous Post
No Comment
Add Comment
comment url