LIST in Python

AboutorderedchangeableduplicateSample
listorderedchangeableallows['abc','def','abc']
tupleorderedunchangeableallows('abc','def','abc')
setunorderedunindexedNot allows{'abc','def','ghi'}
dictionaryunorderedchangeableNot allows{'name':'King','Class':'four'}

Declaring a List

my_list=['Alex','Ronald','John']
print(my_list)
Output
['Alex','Ronald','John']
Displaying elements based on position
my_list=['Alex','Ronald','John']
print(my_list[2])
Output is here
John
index() : Returning the first position of the input element.
my_list=['Ronald','John','Alex','Ravi','Alex']
print(my_list.index('Alex'))
Output is here
2
The position starts from 0 and first element value is my_list[0], so the data at my_list[2] gives us John.
my_list=['Ronald','John','King','Ravi','Alex']
print(my_list[-1]) # last element will be displayed : Alex
print(my_list[-2]) # 2nd from last element will be displayed : Ravi
print(my_list[0:])  # All elements starting from 0 position till end.
print(my_list[:3])  # All elements starting from 0 position till 3.
print(my_list[1:4])  # All elements starting from 1 position till 4.
Output is here
Alex
Ravi
['Ronald', 'John', 'King', 'Ravi', 'Alex']
['Ronald', 'John', 'King']
['John', 'King', 'Ravi']

Displaying all items by looping

We used for loop to display all items of the list.
my_list=['Alex','Ronald','John']
for i in my_list:
    print(i)
Output is here
Alex
Ronald
John

Add , remove , update elements

append()
my_list=['Alex','Ronald','John']
my_list.append('Ravi')
print(my_list)
Output is here
['Alex', 'Ronald', 'John', 'Ravi']
insert(): Adding at location
my_list=['Alex','Ronald','John']
my_list.insert(2,'Ravi')
print(my_list)
Output is here
['Alex', 'Ronald', 'Ravi', 'John']
extend() : Adding elements or list at the end of the list
my_list1=['Alex','Ronald','John']
my_list2=['Ravi','Alen']
my_list1.extend(my_list2)
print(my_list1)
Output is here
['Alex', 'Ronald', 'John', 'Ravi', 'Alen']
copy() : Creates new list by copying all elements
Output is here
['Alex', 'Ronald', 'John']
remove(): Deleting element
my_list=['Alex','Ronald','John']
my_list.remove('Ronald')
print(my_list)
Output is here
['Alex', 'John']
pop(): Deleting element based on location, by default it removes last element
my_list=['Alex','Ronald','John']
my_list.pop()  # deletes last element
print(my_list)
Output is here
['Alex', 'Ronald']
We can remove element from a perticular position
my_list=['Alex','Ronald','John']
my_list.pop(0)  # deletes first element
print(my_list)
Output is here
['Ronald', 'John']
We can collect the removed element
my_list=['Alex','Ronald','John']
name=my_list.pop(1)  # deletes first element
print(name)
Output is here
Ronald
clear() : Removing all elements
my_list=['Alex','Ronald','John']
my_list.clear()  # deletes all elements
print(my_list)
Output is here
[]

Searching element

my_list=['Alex','Ronald','John']
if 'John' in my_list:
  print("Yes, included ")
else:
  print("No, not included")
Output is here
Yes, included
Searching pair of numbers within a list
my_list=[[1,3],[3,4],[4,6]]
if [3,4] in my_list:
    print("yes present")
else:
    print("Not present")
output is here
yes present

Number of elements present:

count():Number of specified element present in a list ( counting the occurrence of a element )
my_list=['Alex','Ronald','John','Alex','Ravi','Alex']
print(my_list.count('Alex'))
Output is here
3
len() : Total Number of elements present in a list
my_list=['Alex','Ronald','John']
print("Number of elements:",len(my_list))
Output
Number of elements: 3
reverse() :Reverse the order of the list
my_list=['Ronald','John','Alex','Ravi']
my_list.reverse()
print(my_list)
Output
['Ravi', 'Alex', 'John', 'Ronald']
sort() : Sort the list in order
my_list=['Ronald','John','Alex','Ravi']
my_list.sort()
print(my_list)
Output
['Alex', 'John', 'Ravi', 'Ronald']
my_list=[4,2,7,1]
my_list.sort()
print(my_list)
Output is here
[1, 2, 4, 7]
MethodDescription
append()Adding element to the list at the end


Your Rating




Post your comments , suggestion , error , requirements etc here




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer