Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. List, Tuples and Dictiona...
  5. Lists
  6. List Operations

List Operations

List Operations

  • Indexing
  • Slicing
  • Concatenation
  • Repetitions
  • Updation
  • Insertion
  • Deletion
  • Membership operator
  • Comparison operator

Creating a list

Creating the list with different type of data items

Example

>>>first_list=[1,"apple",'35.0','Mango','45.0']
>>> print(first_list)
[1, 'apple', '35.0', 'Mango', '45.0']

Indexing

  • Accessing the item in the position 0
  • Accessing the item in the position 1
  • Displays the element at the -1th position
  • Displays the element at the -5th position

Example

>>> print(first_list[0])
1
>>> print(first_list[1])
Apple
>>> print(first_list[-1])
45.0
>>> print(first_list[-5])
1

Slicing

  • Displays the items from the position 1 till the last
  • Displays the items from the position 1 till 4-1
  • Displays the items from -3 till last
  • Displays the items from position – 4 till position (-2-1)

Example

>>> print(first_list[1:])
['apple', '35.0', 'Mango', '45.0']
>>> print(first_list[1:4])
['apple', '35.0', 'Mango']
>>> print(first_list[-3:])
['35.0', 'Mango', '45.0']
>>> print(first_list[-4:-2])
['apple', '35.0']
>>> print(first_list[-1:])
['45.0']

Concatenation Operator (+)

Adding(combining) and printing items of two lists

Example

>>> second_list =[2,"red",'40.0',"yellow",32.5] 
>>> print(first_list +second_list)
[1, 'apple', '35.0', 'Mango', '45.0', 2, 'red', '40.0', 'yellow', 32.5]

Repetition Operator (*)

  • Creates new strings by concatenating multiple copies of the same string
  • [5] is a list with one number, repetion operator * creates 4 copies of 5 and displays in the screen

Syntax

Mylist * n

Example

>>> print(second_list*2)
[2, 'red', '40.0', 'yellow', 32.5, 2, 'red', '40.0', 'yellow', 32.5]
>>> print([5]*4)
[5, 5, 5, 5]

Updating the list

Updating the list using index value

Syntax

Mylist[index] = value

Example

>>> second_list[1] = "green"
>>> print(second_list)
[2, 'green', '40.0', 'yellow', 32.5]

Inserting an element

  • Inserting an element in position 5
  • Inserting an eleemnet in position 3

Syntax

list.insert(index,item)

Example

>>> first_list.insert(5,"Fruits")
>>> print(first_list)
[1, 'apple', '35.0', 'Mango', '45.0', 'Fruits']
>>> first_list.insert(3,"guava")
>>> print(first_list)
[1, 'apple', '35.0', 'guava', 'Mango', '45.0', 'Fruits']

Removing an element

Removing an element by giving the element directly

Syntax

list.remove(item)

Example

>>> first_list.remove("guava")
>>> print(first_list)
[1, 'apple', '35.0', 'Mango', '45.0', 'Fruits']
>>> first_list.remove('45.0')
>>> print(first_list)
[1, 'apple', '35.0', 'Mango', 'Fruits']

Membership Operator (in, not in)

  • The in operator tells the user whether the given string exists in the list or not.
  • It gives a Boolean output, i.e., True or False
  • If the given input is present in the list, then the operator returns true else it returns false
  • The operator not in returns if the input is not present in the list else it will return false

Syntax

item in list
item not in list

Example

>>> MyList1 = [1,2,3,5,9,0]
>>> 2 in MyList1
True
>>> 10 in MyList1
False
>>> MyList2 =[1,2.2,"Python"]
>>> 2.2 in MyList2
True
>>> "Python" in MyList2
True
>>> "Hello" not in MyList2
True
>>> "Python" not in MyList2
False

Comparison Operator (==)

  • The == operator returns true if all the elements in the list are equal else it returns false
  • The != operator returns true if the elements in two lists are different else it returns false

Example

>>> list1 = [2,4,6]
>>> list2 = [2,4,6]
>>> print(list1 == list2)
True
>>> print(list1 != list2)
False

Loading

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments