Tuesday, June 12, 2018

List in Python


What is List?


List is a general data structure widely used in python program. 


In other language referred to as dynamic arrays.



They are both mutable and sequence data type that allows them to be indexed and sliced. 


List contains items separated bye commas(,) and enclosed within square brackets([]). 

Like string list can be accessed using the slice operator([] and [:])



And Indexes starting at ) form beginning of the list and accessing the end using -1. 

Plus(+) sign is the list concatenation operator.

Asterisk(*) is the repetition operators.

Examples:



Creating a list:


myList = [‘abcd’, 9590, 894.78, ‘Justseeker’, True]

# Here myList contain various data type like float, int, bool

print(myList) # Output: [‘abcd’, 9590, 894.78, ‘Justseeker’, True]



Access item from the list


myList = [‘John’, ‘Cherry’, ‘Bob’, ‘Sam’]

# Accessing first item from the list

print(myList[0]) #Output: John

# Accessing List Item from the list

print(myList[-1]) #Output: Sam

# Accessing 1 to 3 items form list

print(myList[1:3]) # Output : [‘Cheery’, ‘Bob]

# Accessing 3 to last


print(myList[3: ]) # Output: ['Sam'] 



In-built function: list()


list() constructor to make a list

myList =list((‘john’, ‘Cherry’, ‘Bob’, ‘Sam’))

print(myList) #Output:['john', 'Cherry', 'Bob', 'Sam'] 


In-built function: append()


Append an item at the of the list

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

myList.append(‘Ruby’)

print(‘myList) #Output:['john', 'Cherry', 'Bob', 'Sam', ‘Ruby’] 


In-bulit function:remove()


Remove item from the list

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

myList.remove("Ruby")

print(mylist) #Output: [‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]


In-built function: len()


myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

To get the length of list

print(len(myList)) # Output: 4



In-built function: clear()


Remove all element from the list

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

print(mylist.clear()) # list is Empty 


In-built function: copy()


myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

copylist = myList.copy()

print(copyList) # Output: [‘john’, ‘Cherry’, ‘Bob’, ‘Sam’] 


In-built function: count()


Count that how many type item repeated

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’, ‘Bob’]

countItem = myList.count(Bob)

print(countItem) # Output: 2 


In-built function: extend()


Extend or append all the item of list one into list second

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

secondList = [‘Apple’, ‘Mango’, ‘Banana’]

myList.extend(secondList)

print(myList) #Output[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’,‘Apple’, ‘Mango’, ‘Banana’]



In-built function: index()


To get index position

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

indexItem = myList.index(‘Cherry’)

print(indexItem) # Output 1 


In-built function: insert()


Adds an element at the specified position

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

myList.insert(1, ‘Ruby’)

print(myList) # Output: [‘Ruby’, ‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]


In-built function: pop()


Removes the element at the specified position

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

value = myList.pop(0)

print(value) # Output: john 


In-built function: reverse()


Reverses the order of the list

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

reverseList = myList.reverse()

print(reverseList) # Output: [‘Sam’, ‘Bob’, ‘Cherry’, ‘john’] 


In-built function: sort()


Sort the list item

myList =[‘john’, ‘Cherry’, ‘Bob’, ‘Sam’]

sortList = myList.sort()

print(sortList) # Output: [‘Bob’, ‘Cherry’, ‘john’, 'Sam']





Prev: Strings in Python

Next: Tuple in Python

No comments:

Post a Comment

Google+ Followers