Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Python list

What is python list?

In python, list is used to store multiple collections of data in one variable. You can store too many values in a list and perform a lot of work on it. You can store string, number, or both at the same time in a list. You can use a capital letter and a small letter in a list. List items are ordered and can have duplicate values. You can access or get a particular item using the index number.

How to create a list in python?

We write list inside a third bracket. So at first give a third bracket[].
To write a string use single or double quotation ('', ").
To write numbers, don't need to use quotations.
Give comma between two values.
If we write the number inside quotation then it will act as a string and we won't be able to perform mathematical work on that list element. If we write numbers and letters or words together then you must give a quotation.

Example:
Input
x=['A','b','C']
y=["1","2","3"]
z=["A","2","c"]
s=[1,2,3]
v=["1A","2b",3]
u=[1,"2b",3,"C"]
print(x)
print(y)
print(z)
print(s)
print(n)
Output
['A','b','C']
["1","2","3"]
["A","2","c"]
[1,2,3]
["1A","2b",3]
u=[1,"2b",3,"C"]

What is index?

Index means the position number of each value which are stored in the list. In python, the index starts from 0. If there are 5 values in the list then its first index is 0 and last the index =(n-1) =(5-1)=4. Index numbers are used in various ways. It's a very important thing. You can get list items using an index number.

Function and Operation of python list

in

To check whether a value is in the list or not, "in" is used.

Example:
Input
x=["a","b","c","d"]
print("a" in x)
Output
True

append() function

To add value to the list append() function is used. This function add a new value at the end of the list.

Example:
Input
x=["a","b","c","d"]
x.append("e")
print(x)
Output
["a","b","c","d","e"]

len() function

To find the length of the list len() function is used. By using len() function you can get the last index number of the list. By finding the last index number you can say that how many values are present in the list.

Example:
Input
x=["a","b","c","d"]
print(len(x))
Output
3

insert() function

To add value to a list in a specific position, insert() function is used. To do this, first pass the index number where we want to add, then give a comma and then pass the value.

Example:
Input
x=["a","b","c","d"]
x.insert(1,"e")
Output
["a","b","e","c","d"]

remove() function

To remove any value from the list, remove() function is used. To remove value pass the value in the function bracket.

Example:
Input
x=["a","b","c","d"]
x.remove("c")
print(x)
Output
["a","b","d"]

sort() function

To sort the data in ascending order, sort() function is used. To sort in descending order, pass a parameter reverse=true in the bracket of sort function.

Example:1
Input
x=[4,1,3,2]
x.sort()
print(x)
Output
[1,2,3,4]
Example:2
Input
x=[4,1,3,2]
x.sort(reverse = True)
print(x)
Output
[4,3,2,1]

reverse() function

To reverse the the list elements, reverse() function is used.

Example:
Input
x=[4,1,3,2]
x.reverse()
print(x)
Output
[2, 3, 1, 4]

See one thing, here in the example, main list element 3 is present in index number 2. But in the output it is in 1 index position. So you can say that you changed the order. This means that you reversed position of the values.

pop() function

To remove a value from end of the list, pop() function is used. If we use pop function one time then it will remove one value from end .Every time we do that, the value of total will decrease from the end .

Example:
Input
x=[1,2,3,4]
x.pop()
print(x)
Output
[1,2,3]

index() function

To find the index number of any value of a list, the index() function is used.

Example:
Input
x=["a","b","c","d"]
y=x.index("c")
print(y)
Output
2

count() function

To see that a selected value how many times present in the list, the count function is used. Pass a value( present in the list) in the function and then it will show that how many times the value present in the list.

Example:
Input
x=["a","b","c","d","c"]
y=x.count("c")
print(y)
Output
2

copy() function

To copy a list copy() function is used.

Example:
Input
v= ['apple','cherry', 'orange']
x= v.copy()
print(x)
Output
['apple', 'cherry', 'orange']

clear() function

To remove all the elements present in a list at once, the clear function is used.

Example:
Input
v = ['apple', 'banana', 'orange']
x = v.copy()
print(x)
Output
None

Slicing

By index number, we can get values from the list. Negative indexing means starting from the end of the list. The working process is same as what we saw in the string slicing.

Example:1
Input
x=["a","b","c","d","e"]
y=x[2]
z=x[-2]
print(y)
print(z)
Output
c
d
Example:2
Input
x=["a","b","c","d","e"]
y=x[2:]
z=x[:-1]
print(y)
print(z)
Output
["c","d","e"]
["a","b","c","d"]
Example:3
Input
x=["a","b","c","d","e","w","q","z","m"]
y=x[2:5]
z=x[-5:-1]
print(y)
print(z)
Output
['c', 'd', 'e']
['e', 'w', 'q', 'z']

How to Change value of list using index number?

Example:1
Input
x=["a","b","c","d"]
x[2]="Rafsun"
print(x)
Output
['a', 'b','Rafsun','d']
Example:2
Input
x=["a","b","c","d"]
x[1:3]=["Rafsun"]
print(x)
Output
['a', 'Rafsun', 'd']
Example:3
Input
x=["a","b","c","d"]
x[1:2]="Rafsun"
print(x)
Output
['a', 'R', 'a', 'f', 's', 'u', 'n', 'c', 'd']
Example:4
Input
x=["a","b","c","d"]
x[1:2]=["Rafsun","Ahmad"]
print(x)
Output
['a', 'Rafsun', 'Ahmad', 'c', 'd']

How to join list in python?

You can join multiple lists together and can create a new list

Example:
Input
x=["a","b","c","d"]
y=[1,2,3,4]
z=x+y
print(z)
Output
["a","b","c","d",1,2,3,4]

List Comprehension

You will understand this concept better after learning about loops and conditions. I will recommend you to learn about loops and conditions before learning list comprehension. You can learn about loops and conditions from the next lecture. List comprehension helps us to create a shorter syntax when we want to create a new list based on the values of an existing list.
Basic syntax: newlist = [expression for item in iterable if condition == True]

Let's see an example:
Based on a list of numbers, create a new list. This new list will contain only the even numbers from the first list. Without list comprehension, you have to write a for statement with a conditional test inside.

Without using list comprehension

Example:
Input
number = [1,2,3,4]
newlist = []
for x in number :
      if x%2==0:
             newlist.append(x)
print(newlist)
Output
2 ,4

Using list comprehension

Example:1
Input
number = [1,2,3,4]
newlist = [x for x in number if x%2==0 ]
print(newlist)
Output
2 ,4

In comprehension bracket using the if condition, we found all the even number from the first list and then using for loop we took those values in the x variable, one by one and then print the value of x. Now if we compare both examples then we can understand that list comprehension makes the code shorter.

Example:2
Input
list=[1,2,3,4,5,6]
mew_list=[i**2 if i%2==0 else -i for i in list]
print(new_list)
Output
[-1,4,-3,16,-5,36]

What we did here, we do square of even numbers present in the list and odd number as negative odd.What we did here, we do square of even numbers present in list and odd number as negative odd.

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.