Python List and Basic Python Set method

When we talk of different types of collection Python list is most important which can be written as a list of values items between square brackets well separated by commas.

An interesting fact about a list is that it is not necessary that items in a list should be of the same type.

Example-

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = [1, 2, 3, 4, 5 ];

list3 = ["a", "b", "c", "d"];

Accessing Values in Lists-

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];

print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])

Output-

list1[0]: physics
list2[1:5]: [2, 3, 4, 5]

 

Updating Lists-

list = ['physics', 'chemistry', 1997, 2000];
print ("Value available at index 2 : ")
print (list[2])
list[2] = 2001;
print ("New value available at index 2 : ")
print (list[2])

Output-

Value available at index 2 : 
1997
New value available at index 2 : 
2001

Python Set Methods-

Method                                            Description

  1. add()                            Adds an element to the set
  2. clear()                          Removes all element from the set
  3. copy()                          Returns a copy of the set
  4. discard()                     Remove the specified item
  5. pop()                           Removes an element from the set
  6. remove()                    Removes an element from the set
  7. union()                       Return a set containing the union of sets
  8. update()                     Update the set with union of this set and others

 

Examples-

1.add()-

forms = {"MIDNIGHT", "MIDDAY", "DUSK"}

forms.add("DAWN") 

print(forms)

Output-

{'MIDNIGHT', 'MIDDAY', 'DUSK', 'DAWN'}

2.clear()-

forms = {"MIDNIGHT", "MIDDAY", "DUSK"}

forms.clear() 

print(forms)

3.copy()-

forms = {"MIDNIGHT", "MIDDAY", "DUSK"}

form2=forms.copy() 

print(form2)

Output-

{'MIDDAY', 'DUSK', 'MIDNIGHT'}

4.discard()-

forms = {"MIDNIGHT", "MIDDAY", "DUSK"}

forms.discard("DUSK") 

print(forms)

Output-

{'MIDNIGHT', 'MIDDAY'}

5.pop()-

forms = {"MIDNIGHT", "MIDDAY", "DUSK"}

forms.pop()

print(forms)

Output-

{'MIDNIGHT', 'MIDDAY'}

6.remove()-

forms = {"MIDNIGHT", "MIDDAY", "DUSK"}

forms.remove("DUSK")

print(forms)

Output-

{'MIDNIGHT', 'MIDDAY'}

 

7.union()-

forms = {"MIDNIGHT", "MIDDAY", "DUSK"}
forms2={"Fairy", "Dark", "Ice"}

forms3 =forms.union(forms2)

print(forms3)

Output-

{'MIDDAY', 'DUSK', 'MIDNIGHT', 'Fairy', 'Ice', 'Dark'}

8.update()-

forms = {"MIDNIGHT", "MIDDAY", "DUSK"}
forms2={"Fairy", "Dark", "Ice"}

forms.update(forms2)

print(forms)

Output-

{'Fairy', 'Ice', 'MIDDAY', 'Dark', 'DUSK', 'MIDNIGHT'}

 

More Examples-

#To find the max values in the list

list1, list2 = ['xya', 'xyz', 'zara', 'abc'], [456, 700, 200]

print ("Max value element : ", max(list1))
print ("Max value element : ", max(list2))

#To find the min values in the list

list1, list2 = ['xya', 'xyz', 'zara', 'abc'], [456, 700, 200]

print ("Min value element : ", min(list1))
print ("Min value element : ", min(list2))

Output-

Max value element : zara
Max value element : 700
Min value element : abc
Min value element : 200
#reverse list

aList = [123, 'xyz', 'zara', 'abc', 'xyz'];

aList.reverse();
print ("List : ", aList);

Output-

List : ['xyz', 'abc', 'zara', 'xyz', 123]

 

Leave a Reply

Your email address will not be published. Required fields are marked *