All about "list" in python

in #new6 years ago

Today we discuss about most versatile datatype available in Python called list.
list is a comma-separated values between square brackets as below . Important thing about a list is that items in a list need not be of the same type.
Let's see how to create a list
Example:
thislist = ["apple", "banana", "cherry"]

That's it, it's that simple to create a list in python.

How to Access Values in Lists
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. For example −

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]

When the above code is executed, it produces the following result −

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

Now, we quickly see the list methods :

List Methods
Python has a set of built-in methods that you can use on lists.

append()
Adds an element at the end of the list
Eg- list. Append()

clear()
Removes all the elements from the list
Eg- list. clear()

copy()
Returns a copy of the list
list2=list. Copy()

count()
Returns the number of elements with the specified value
Length=list. count()

append()
Add the elements of a list (or any iterable), to the end of the current list
list. append()

index()
Returns the index of the first element with the specified value
Ind=list. index()

insert()
Adds an element at the specified position
list. insert(index, value)

pop()
Removes the element at the specified position
list. pop(index)

remove()
Removes the first item with the specified value
list. remove(value)

reverse()
Reverses the order of the list
list. reverse()

sort()
Sorts the list
list. sort()

Now lets see what Basic Operations we can do with lists -------
IMG_20180702_012716.png
Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.
python-training-500x500.png

Sort:  

meri voting power kam ho gyi hai me abhi aapko upvote nhi kar sakta .ab kal hi upvote kar paunga

Friends 7239809081 is whatsapp no. pe daily apni post ki link share karna me uspe upvote or comment karunga or aapko me bhejunga aap meri post pe karna

Overall very good informatic post. Keep it up.

Coin Marketplace

STEEM 0.25
TRX 0.25
JST 0.040
BTC 94006.35
ETH 3379.28
USDT 1.00
SBD 3.47