Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

type([])

list

list create [] (changeble,allow duplicate value, ordered) access (index,slice) operation (inser
tuple create access add delete operation
set create access add delete operation
dictionary create access add delete operation

List

Lists are used to store multiple items in a single variable.

lists are created using square brackets: [ ]

A sequence type is formed by putting together some other types in a sequence.

Sequence types such as lists, tuples, and strings are always ordered

list is an ordered sequence of items

list contains different types of data items stored in the list seperated with commas(,) enclosed within sqaure bracket[ ]

[1 ,3 ,4 ,1 ,6,"a"]
[1, 3, 4, 1, 6, 'a']

type( [1 ,3 ,4 ,1 ,6] )
list

# list allow duplicate value, consider all items for length


fruit_list=["Apple","mango","Orange","Grapes","mango","Bananas"]#list
print("List:",fruit_list)
print(type(fruit_list))
print(len(fruit_list))
List: ['Apple', 'mango', 'Orange', 'Grapes', 'mango', 'Bananas']
<class 'list'>
6

#set dont allow duplicate value, consider length one of same items
fruits={"apple","banana","cherry","cherry","apple"}

print(fruits)
print(len(fruits))
{'banana', 'apple', 'cherry'}
3

List items

List items are ordered,changeble,and allow duplicate values.

List items are inedexed, the first item has index[0], the second item has index[1] etc.

Access items

List items are indexed, and you can access them by referring to the index numbers.

we can use[:]operators to access the data of the list

# 0 1 2 3 4
fruit_list=["Apple","Mango","Orange","Grapes","Bananas"]#list
print("List:",fruit_list[3])
List: Grapes

Negative Indexing
# -5 -4 -3 -2 -1
fruit_list=["Apple","Mango","Orange","Grapes","Bananas"]#list
print("List:",fruit_list[-4])
List: Mango

slice

# -5 -4 -3 -2 -1
fruit_list=["Apple","Mango","Orange","Grapes","Bananas"]#list
print("List:",fruit_list[:-2]) # start 0 end -2 --- 0 to -3
List: ['Apple', 'Mango', 'Orange']

Check if item exists

To determine if a specified item is present in a list use the in keyword

fruit_list=["Apple","Mango","Orange","Grapes","Bananas"]#list
if "Apple" in fruit_list:
print("Exists!")
print("hello")
#{} indentation
Exists!
hello

fruit_list=["Apple","Mango","Orange","Grapes","Bananas","Watermelon"]#list
if "kiwi" in fruit_list:
print("Exists!")
print("hello")
hello

#index0 1 2 3 4 5
# 1 2 3 4 5 6
list=[10,20,30, 'ten','twenty','thirty']
print("complete list:",list)
print("4th element from the list list[3]=", list[3])
print("element from 1 to 3 list[0:3]=", list[0:3])
print("element from 4 to 5 list[3:5]=", list[3:5])
print("element from 6 to end list[4:]=", list[4:])
complete list: [10, 20, 30, 'ten', 'twenty', 'thirty']
4th element from the list list[3]= ten
element from 1 to 3 list[0:3]= [10, 20, 30]
element from 4 to 5 list[3:5]= ['ten', 'twenty']
element from 6 to end list[4:]= ['twenty', 'thirty']

fruits=["apple","banana","cherry","pineapple","guava"]
if "guava" in fruits:
print("guava is present")
guava is present

Change item value

To change the value of a specific item, refer to the index numbers.

# 0 1 2 3 4
fruits=["apple","banana","cherry","pineapple","guava"]

fruits[1]="watermelon"

print(fruits)
['apple', 'watermelon', 'cherry', 'pineapple', 'guava']
fruits=["apple","banana","cherry","pineapple","guava"]

fruits[1]="watermelon"

fruits[2]="banana"
print(fruits)
['apple', 'watermelon', 'banana', 'pineapple', 'guava']

Change a range of item values

To change the value of item within a specific range , define a list with the new values and refer to the range of index numbers where you want to
insert new values.

If you insert more item then you replace , the new items will be inserted where you specified, and the remaining items will move accordingly.

# 0 1 2 3 4
fruits=["apple","banana","cherry","pineapple","guava"]

newfruits=["watermelon","muskmelon"]
print(fruits[1:3])
fruits[1:3]=newfruits
print(fruits[1:3])
print(fruits)
['banana', 'cherry']
['watermelon', 'muskmelon']
['apple', 'watermelon', 'muskmelon', 'pineapple', 'guava']

2 items change with 3 items

# 0 1 2 3 4
fruits=["apple","banana","cherry","pineapple","guava"]

newfruits=["watermelon","kiwi","peach"]

fruits[1:3]=newfruits
print(fruits)
['apple', 'watermelon', 'kiwi', 'peach', 'pineapple', 'guava']

# 0 1 2 3 4
fruits=["apple","banana","cherry","pineapple","guava"]

newfruits=["watermelon","kiwi","peach"]

fruits[1]=newfruits
print(fruits)
['apple', ['watermelon', 'kiwi', 'peach'], 'cherry', 'pineapple', 'guava']

fruits=["apple","banana","cherry","pineapple","guava"]

newfruits=["watermelon","kiwi","peach"]

fruits[2]=newfruits
print(fruits)
['apple', 'banana', ['watermelon', 'kiwi', 'peach'], 'pineapple', 'guava']

insert operation
# -5 -4 -3 -2 -1
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(-1,"watermelon")

print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'watermelon', 'guava']

# 0 1 2 3 4
# -5 -4 -3 -2 -1
fruits=["apple","banana","cherry","pineapple","guava"]
print(fruits)
print(len(fruits))
fruits.insert(-3,"watermelon")
print(fruits)
print(len(fruits))
['apple', 'banana', 'cherry', 'pineapple', 'guava']
5
['apple', 'banana', 'watermelon', 'cherry', 'pineapple', 'guava']
6

fruits=["apple","banana","apple","cherry","pineapple","guava"]
print(len(fruits))
6

Insert Items

To insert a new list item, without replacing any of the existing value, we can use the insert() method.

The insert() method inserts an item at the specified index.

fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(5,"watermelon")

print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'guava', 'watermelon']

fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(4,"watermelon")

print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'watermelon', 'guava']

fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(-2,"watermelon")

print(fruits)
['apple', 'banana', 'cherry', 'watermelon', 'pineapple', 'guava']

fruits=["apple","banana","cherry","pineapple","guava"]
fruits.insert(-1,"watermelon")

print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'watermelon', 'guava']

Append Item

keyboard_arrow_down add at the end


To add an item to the end of the list, use the append()method.
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.append("watermelon")
print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'guava', 'watermelon']

Extend List

To append elements from another list to the current list use the extend() method.

fruits=["apple","banana","cherry","pineapple","guava"]
newfruits=["watermelon","peaches"]
fruits.extend(newfruits)
print(fruits)
['apple', 'banana', 'cherry', 'pineapple', 'guava', 'watermelon', 'peaches']

Remove Specified item

The remove() method removes the specified item

fruits=["apple","banana","cherry","pineapple","guava"]
fruits.remove("banana")
print(fruits)
['apple', 'cherry', 'pineapple', 'guava']

Remove specified index

The pop() method removes the specified index.

if you do not specify the index, the pop() method removes the last item.

fruits=["apple","banana","cherry","pineapple","guava"]
fruits.pop(1)
print(fruits)
['apple', 'cherry', 'pineapple', 'guava']

fruits=["apple","banana","cherry","pineapple","guava"]
fruits.pop()
print(fruits)
['apple', 'banana', 'cherry', 'pineapple']

The del keyword also removes the specified index.

The del keyword can also delete the list items compeletely and in the output it gives a empty list

fruits=["apple","banana","cherry","pineapple","guava"]
del fruits[0]
print(fruits)
['banana', 'cherry', 'pineapple', 'guava']

# The del keyword can also delete the list items compeletely and in the output it gives a empty
fruits=["apple","banana","cherry","pineapple","guava"]
del fruits[:]
print(fruits)
[]

Clear the list

The clear method empties the list. The list still remains, but it has no content.
fruits=["apple","banana","cherry","pineapple","guava"]
fruits.clear()
print(fruits)
[]

list create access add delete operation


tuple create access add delete operation
set create access add delete operation
dictionary create access add delete operation

You might also like