Python sequences and lists

Sequences and Lists in Python

Sequences and Lists in Python

For creating a list, just put values between the square bracket and separated by commas.

list1 = [1,2,3,4] list2 = ["Sonarika","Vishal","Vijay","Aditya"] list3 = ['h','e','l','l','0'] list4 = [4,"Jiten",'a',5]

Accessing list

For accessing values of the list, put index position inside square brackets.

list-variablename[start-index : end-index]

start-index is from where to start selection, and end-index is index position where selection ends.

list1 = [1,2,3,4] list2 = ["Sonarika","Vishal","Vijay","Aditya"] print("list1[2] : ", list1[2]) print("list2[1:3] : ", list2[1:3]) print("list1[-2] : ",list1[-2])

When we execute the above program, it will produce the following output –

list1[2] : 3 list2[1:3] : ['Vishal', 'Vijay'] list1[-2] : 3

Updating list

For updating list, select index position and assign value using assignment operator (=).

list1 = [1,2,3,4] list1[2] = 11 print(list1)

When we execute the above program, it will produce the following output –

Deleting list item

In Python, we can use del statement for deleting existing list or delete list item by index.

del list-name or del list-name[index]

list1 = [1,2,3,4] del list1[2] print(list1)

List Functions

1. append – Adding a new value to existing list.
Syntax – append(value)

list1 = [1,2,3,4] list1.append(32) print(list1)

2. count – Return the occurrence of value in existing list.
Syntax count(value)

Example

list1 = [1,2,3,4,3,43] c = list1.count(3) print(c)

3. clear – Clear the existing list values.
Syntax clear()

Example

list1 = [43,3,22,11,54] list1.clear() print(list1)

4. extend – Adding sequence to existing list.
Syntax extend(sequence)

Example

list1 = [1,2,3,4] list1.extend([5,6]) print(list1)

5. insert – Insert value at given index.
Syntax insert(index, value)

Example

list1 = [1,2,3,4] list1.insert(2,54) list1.insert(5,11) print(list1)

6. index – Return the lowest index position of a value.
Syntax index(value)

Example

list1 = [1,2,3,2,4] a = list1.index(2) print("position : ",a)

7. pop – Remove the existing list value by index and return removing index value.
Syntax pop(index)

Example

list1 = [1,2,3,4] print(list1[2]) print(list1)

8. remove – Its also remove value from existing list by index, but it doesn’t return any value.
Syntax remove(index)

Example

list1 = [55,21,3,13,7] list1.remove(2) print(list1)

9. reverse – Reverse the existing list.
Syntax reverse()

Example

list1 = [1,2,3,4] list1.reverse() print(list1)

10. sort – Sort the existing list.
Syntax sort()

Example

list1 = [43,3,22,11,54] list1.sort() print(list1)

Some others Functions

1. len – Return the total length of a list.

Syntax len(list)

Example

list1 = [43,3,22,11,54] print("Length : ",len(list1))

2. max – Return the max value from a list.

Syntax max(list)

Example

list1 = [43,3,22,11,54] print("Max : ",max(list1))

3. min – Return the min value from a list.

Syntax min(list)

Example

list1 = [43,3,22,11,54] print("Min : ",min(list1))

4. list – Convert a tuple into a list.

Syntax list(seq)

Example

tuple1 = (43,3,22,11,54) list1 = list(tuple1) print("tuple1 : ",tuple1) print("list1 : ",list1)
tuple1 : (43,3,22,11,54) list1 : [43,3,22,11,54]

2. Tuple

  • For creating a new tuple use parentheses.
  • In tuple, it is not necessary that all values should be of same datatype (i.e, int, float, string,etc).
  • Tuples are immutable or we can’t modify values of a tuple.

Creating a list

For creating a tuple, just put values between parentheses and separated by commas.

Accessing a tuple

For accessing values of a tuple, put index position inside square brackets.

tuple-variablename[start-index : end-index]

start-index is from where to start selection, and end-index is index position where selection ends.

tuple1 = [22,1,34,54] tuple2 = ["Sonarika","Vishal","Vijay","Aditya"] print("tuple1[2] : ", tuple1[2]) print("tuple2[1:3] : ", tuple2[1:3]) print("tuple1[-2] : ",tuple1[-2])

When we execute the above program, it will produce the following output –

tuple1[2] : 3 tuple2[1:3] : ['Vishal', 'Vijay'] tuple1[-2] : 34

Tuple Functions

1. count – Return the occurrence of value in existing tuple.
Syntax count(value)

Example

tuple1 = [12,1,54,41,1] print("count : ",tuple1.count(1))

2. index – Return the lowest index position of a value.
Syntax index(value)

Example

tuple1 = [12,1,54,41,1] print("position : ",tuple1.index(1))

Some others Functions

1. len – Return total length of a tuple.

Syntax len(tuple)

Example

tuple1 = (43,3,22,11,54) print("Length : ",len(tuple1))

2. max – Return max value from tuple.

Syntax max(tuple)

Example

tuple1 = [43,3,22,11,54] print("Max : ",max(tuple1))

3. min – Return min value from tuple.

Syntax min(tuple)

Example

tuple1 = [43,3,22,11,54] print("Min : ",min(tuple1))

4. tuple – Convert a list into a tuple.

Syntax tuple(seq)

Example

list1 = [43,3,22,11,54] tuple1 = tuple(list1) print("list1 : ",list1) print("tuple1 : ",tuple1)
list1 : [43,3,22,11,54] tuple1 : (43,3,22,11,54)

Источник

Читайте также:  Coordinate system in python
Оцените статью