List Data Type in Python and Methods of List

 

 List  Data Type in Python and Methods of List

 List is a data structure or data type which is used to store the data items in ordered manner. List is a collection of data items that are ordered and changeable. The list data items are written using pair of square bracket and each item is separated by comma. By using list, you can store multiple data items or values in a variable. In list we can write any type of data item.

List is mutable it means list items can be modified after creating it.

In Python, the list is a built-in data type that represents a mutable, ordered sequence of elements. Lists are versatile and widely used for storing collections of items. Here are some key characteristics and operations associated with the list data type:


Mutable:

Lists are mutable, meaning you can modify their elements by assigning new values or using various list methods.

Ordered:

Lists maintain the order in which elements are inserted. The order of elements is preserved when iterating through the list.

Dynamic:

Lists can dynamically grow or shrink in size. You can add or remove elements as needed.

Heterogeneous Elements:

Lists can contain elements of different data types, including numbers, strings, other lists, or even a mix of data types.

Indexing and Slicing:

Elements in a list are accessed using indexes. You can also use slicing to extract sub lists from a list.

Common Operations:

Various operations can be performed on lists, such as appending elements, extending lists, concatenating lists, sorting, reversing, and more.

 Syntax to create List:

List1=[1,2,3,4,5,6,7,8,9]

List1 ia a list variable which contain total 9 elements which have integer data type.

List2=[“hello”,”students”,”how”,”are”,”you”]

In this example List2 is string list.

List3=[10.5,20.10,30.6]

The above list is containing floating data elements.


How to take user input for list:

list1= input("Enter a list elements: ").split(',')

print("Your lis is:",list1)

Output:

Enter a list elements: hello friends how are yoy

Your lis is: ['hello friends how are yoy']


Methods of List in Python:

Following are some methods used to perform operations on list

1.     append( x):

The append() method is used to add an element at the end of list.

For example

list1=[1,3,8,9,1000]

print("List before appending element:",list1)

list1.append(2000)

print("List after appending element:",list1)Output:

List before appending element: [1, 3, 8, 9, 1000]

List after appending element: [1, 3, 8, 9, 1000, 2000]

 

Another example,

my_list=["Books","Pens","Laptop","Computer"]

print("List before appending the element:",my_list)

my_list.append("Notes")

print("List after appendong the lement:",my_list)

Output:

List before appending the element: ['Books', 'Pens', 'Laptop', 'Computer']

List after appendong the lement: ['Books', 'Pens', 'Laptop', 'Computer', 'Notes']


2.     insert(I,x):

The insert function is used to add an element in the list at any position or at a specific index in a list.

For example,

list2=["hello","friends","good","morning"]

print("Before inserting the element in list:",list2)

list2.insert(3,"students")

print("After inserting the element in a list at 3rd index:",list2)

Output:

Before inserting the element in list: ['hello', 'friends', 'good', 'morning']

After inserting the element in a list at 3rd index: ['hello', 'friends', 'good', 'students', 'morning']

 

Another example,

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

print("Before inserting the element in list:",list1)

list1.insert(0,0)

print("After inserting the element at 0th index:",list1)

Output:

Before inserting the element in list: [1, 2, 3, 4, 5]

After inserting the element at 0th index: [0, 1, 2, 3, 4, 5]

 

3.     extend(iterable):

extend() method is used to add the elements to a list. This method helps to add group of items at the end of list.

The difference between the append, insert and extend method is,

Append() is used to add a single element to the end of list

The insert() method is useful to insert the elements at a specific position  or index

Extend() method is used to add group of elements at the end of list

For example,

list1=[2,4,6,8]

list2=[10,12,14,16]

print("Before extending list is:",list1)

list1.extend(list2)

print("After extending the:",list1)

Output:

Before extending list is: [2, 4, 6, 8]

After extending the: [2, 4, 6, 8, 10, 12, 14, 16]

 

Another example,

list1=["hello","students"]

list2=["good","morning"]

print("Before extending the list: ",list1)

list1.extend(list2)

print("The extended list is:",list1)

Output:

Before extending the list:  ['hello', 'students']

The extended list is: ['hello', 'students', 'good', 'morning']

4.     remove(x):

remove() method is used to remove the element from list. It removes first occurance of specified element from given list. This method takes one argument where the element you want to remove.

For example,

list1=[1,2,3,4,5,6,7,8,5,8,5,8,5,8] # in case of similar items only first occurance will be removed

print("Before removing the list element:",list1)

list1.remove(5)

print("After removing 5 from list:",list1)

Output:

Before removing the list element: [1, 2, 3, 4, 5, 6, 7, 8, 5, 8, 5, 8, 5, 8]

After removing 5 from list: [1, 2, 3, 4, 6, 7, 8, 5, 8, 5, 8, 5, 8]

Another Example,

list1=["hello","friends","good","morning"] # in case of similar items only first occurance will be removed

print("Before removing the list element:",list1)

list1.remove("morning")

print("After removing  from list:",list1)

Output:

Before removing the list element: ['hello', 'friends', 'good', 'morning']

After removing  from list: ['hello', 'friends', 'good']

5.     pop():

pop() is used to remove the elements with its specific index.

For example,

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

print("Before removing the elemet on specific index:",list1)

list1.pop(3) # here 3 is index not data element

print("After removing the element from specific index:",list1)

Output:

Before removing the elemet on specific index: [1, 2, 3, 4, 5, 6, 7]

After removing the element from specific index: [1, 2, 3, 5, 6, 7]

Another example,

list1=["hello","students","good","morning"]

print("Before removing string from list:",list1)

list1.pop(2)

print("After removing string from list:",list1)

Output:

Before removing string from list: ['hello', 'students', 'good', 'morning']

After removing string from list: ['hello', 'students', 'morning']

6.     index(i):

index(i) method returns the index of first occurrence of element from list.

For example,

list1=["hello","good","morning"]

list1.index("good")

Output:

1

Another example,

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

print("List is:",list1)

list1.index(2)

Output:

1

7.     count(x):

count() method will return the count of occurrences of a element in a list.

For example,

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

list1.count(3)

Output:

2

Another example,

list1=["hello","good","morning"]

list1.count("friends")

Output:

0

8.     sort():

sort() function in python used to sort the elements of list in ascending order.

For example,

list1=[1,4,9,3,2]

print("The list elements before sort:",list1)

list1.sort()

print("The list elements after sorting:",list1)

 

Output:

The list elements before sort: [1, 4, 9, 3, 2]

The list elements after sorting: [1, 2, 3, 4, 9]

 

Another example,

list1=["hello","firends","good","morning"]

print("The list elements before sorting:",list1)

list1.sort()

print("The list after sorting:",list1)

 

Output:

The list elements before sorting: ['hello', 'firends', 'good', 'morning']

The list after sorting: ['firends', 'good', 'hello', 'morning']

 

9.     reverse():

The reverse function is used to reverse the order  of elements of list.

For example,

list1=[1,2,3,4,5,6]

print("Original list:",list1)

list1.reverse()

print("The reverse order list is:",list1)

Output:

Original list: [1, 2, 3, 4, 5, 6]

The reverse order list is: [6, 5, 4, 3, 2, 1]

Another example,

10.     clear():

clear() is used to remove all elements from list.

For example,

list1=[1,2,3,4,5,6]

print("Original list:",list1)

list1.clear()

print("The clearing the list is:",list1)

Output:

Original list: [1, 2, 3, 4, 5, 6]

The clearing the list is: []

टिप्पणी पोस्ट करा

0 टिप्पण्या