EMAIL SUPPORT

dclessons@dclessons.com

LOCATION

US

Working on Advance Lists

Working on Advance Lists

Lists Concatenation

We can use + sign to concatenate two lists, Lets understand this by an example

Let’s suppose we have two lists mentioned below

A = [ 10,20,30,40]

B = [50,60,70,80]

Now to concatenate two lists we can use + sign

print(A+B)

Output: [10,20,30,40,50,60,70,80]

List Repetition

We can repeat the list elements ‘n’ number of times using * operator. Let’s suppose if we want to repeat the List Elements 2 times then we can write like x*2, whereas x is List.

If X = [10,20,30,40]

print(X*2)

Output: [10,20,30,40, 10,20,30,40]

Lists Membership

There is also possible to check weather an element is a member of list or not. This can be done by ‘in’ and ‘not in” operator.

If the element is in list then it will return TRUE and it will return false. And “not in “operator return the TRUE if an element is not in list else it will return false.

Lets understand this by an example

Example 1: X = [1,2,3,4,5,6,7,8]

B = 7

print(B in X)

Output: True

print( B not in X)

Output: False

How to make Alias and Clone of any List

Aliasing a list

When we give a new name to any existing List, it will create an alias of it.

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

B = A

Now in this case, we are having a list with two names. And if we edit in any of the List, say on A then the change will also reflect in B and vice Versa.

Let’s suppose we change the element value 8 in A [1] then observe what will happen

A[1] = 8

print(A)

print(B)

Output:

A = 1,8,3,4,5

B = 1,8,3,4,5

Cloning a List

In this when we clone any list and if we do any change in any list then its clone will not reflect that particular change and vice versa.


Comment

    You are will be the first.

LEAVE A COMMENT

Please login here to comment.