(Part 1)

#Use of operators in List:

Operators are used for modifications in lists. We’ll be using the + and * operators and their compound forms += and *=.

The + operator can be used to concatenate two or more lists together:

LetsUpgrade_Courses = ['Java', 'ML', 'AI', 'Data Science', 'Javascript']

prizes= ['Hp Laptop', 'LED TV', 'Iphone']

print(LetsUpgrade_Courses + prizes)

Output

['Java', 'ML', 'AI', 'Data Science', 'Javascript','Hp Laptop', 'LED TV', 'Iphone']

Now, let’s look at * operator:

print(LetsUpgrade_Courses * 2)

print(prizes* 3)

Output

['Java', 'ML', 'AI', 'Data Science', 'Javascript','Java', 'ML', 'AI', 'Data Science', 'Javascript']

['Hp Laptop', 'LED TV', 'Iphone','Hp Laptop', 'LED TV', 'Iphone','Hp Laptop', 'LED TV', 'Iphone']

Compound forms '+= ‘ & ‘ *= ‘ :

for x in range(1,4):

    LetsUpgrade_Courses += ['AI']

    print(LetsUpgrade_Courses)

1

2

3

4

Output

['Java', 'ML', 'AI', 'Data Science', 'Javascript', 'AI']

['Java', 'ML', 'AI', 'Data Science', 'Javascript', 'AI', 'AI']

['Java', 'ML', 'AI', 'Data Science', 'Javascript', 'AI', 'AI', 'AI']

1

2

3

4

5

prizes = ['bike']

for x in range(1,4):

    prizes *= 2

    print(prizes)

1

2

3

4

Output

['bike', 'bike']

['bike', 'bike', 'bike', 'bike']

['bike', 'bike', 'bike', 'bike', 'bike', 'bike', 'bike', 'bike']

#Methods used in List:

Now, your work is to GOOGLE the below functions and understand how they work in Python Programming. (Exercise)

FUNCTIONDESCRIPTION
Append()Add an element to the end of the list
Extend()Add all elements of a list to the another list
Insert()Insert an item at the defined index
Remove()Removes an item from the list
Pop()Removes and returns an element at the given index
Clear()Removes all items from the list
Index()Returns the index of the first matched item
Count()Returns the count of number of items passed as an argument
Sort()Sort items in a list in ascending order
Reverse()Reverse the order of items in the list
copy()Returns a copy of the list

#Built-in functions in List:

FUNCTIONDESCRIPTION
reduce()Apply a particular function passed in its argument to all of the list elements stores the intermediate result and only returns the final summation value
sum()Sums up the numbers in the list
ord()Returns an integer representing the Unicode code point of the given Unicode character
cmp()This function returns 1, if first list is “greater” than second list
max()return maximum element of given list
min()return minimum element of given list
all()Returns true if all element are true or if list is empty
any()return true if any element of the list is true. if list is empty, return false
len()Returns length of the list or size of the list
enumerate()Returns enumerate object of list
accumulate()apply a particular function passed in its argument to all of the list elements returns a list containing the intermediate results
filter()tests if each element of a list true or not
map()returns a list of the results after applying the given function to each item of a given iterable
lambda()This function can have any number of arguments but only one expression, which is evaluated and returned.

#Deleting Elements from List:

To remove the item, we’ll use the del statement then call the list variable and the index number of that item:

1

2

3

4

LetsUpgrade_Courses =['Java', 'ML', 'AI', 'Data Science', 'Javascript']

del LetsUpgrade_Courses[2]

print(LetsUpgrade_Courses)

1

2

Output

['Java', 'ML', 'Data Science', 'Javascript']

To remove more than one element, you can use del with a slice index:

1

2

3

4

>>> t = ['a', 'b', 'c', 'd', 'e', 'f']

>>> del t[1:5]

>>> print(t)

['a', 'f']

There are several other ways to delete elements from a list. If you know the index of the element you want, you can use pop and remove:

1

2

3

4

5

6

>>> t = ['a', 'b', 'c']

>>> x = t.pop(1)

>>> print(t)

['a', 'c']

>>> print(x)

b

pop modifies the list and returns the element that was removed. If you don’t provide an index, it deletes and returns the last element.

If you know the element you want to remove (but not the index), you can use remove:

1

2

3

4

>>> t = ['a', 'b', 'c']

>>> t.remove('b')

>>> print(t)

['a', 'c']

#Constructing a List with List Items:

1LetsUpgrade_Community = [['Frontend', 'Backend'],['Sai Sir', 'Viral Sir', 'Prasad Sawant', 'Annirudha', 'Yogesh Raghupati']]

The lists within a list are called nested list.

To access an item within this list, we will have to use multiple indices:

1

2

print(LetsUpgrade_Community[1][0])

print(LetsUpgrade_Community[0][0])

1

2

3

Output

Sai Sir

Frontend

(Part 3)