You are on page 1of 5

12/30/21, 12:05 PM 3 List

List

1) List is collection of elements


2) Purpose of list is to group up the things which fall under same
category 3) List is one of the Data Structure
4) list is enclosed in []
5) lists are mutable , we can
change the values in lists

In [2]:
a = [2,3,4,5,6]

In [3]:
type(a)

Out[3]: list

In [4]:
#we can also create the list with different data structure

b =[2,3,"hello","welcome",True, False]

Out[4]: [2, 3, 'hello', 'welcome', True, False]

In [5]:
# In list indexing remain same it start with 0

b[0]

Out[5]: 2

In [6]:
b[2:4]

Out[6]: ['hello', 'welcome']

In [7]:
b[2:]

Out[7]: ['hello', 'welcome', True, False]

In [8]:
b[-1]

Out[8]: False

In [9]:
# Find the lenght of List

len(a)

Out[9]: 5

In [2]:
# changing the value in list

a =[2,3,4,5,6]

file:///C:/Users/rgandyala/Downloads/3 List.html 1/5


12/30/21, 12:05 PM 3 List

Out[2]: [2, 3, 4, 5, 6]

In [13]:
a[2] = 10

In [14]:
a

Out[14]: [2, 3, 10, 5, 6]

In [15]:
# LIST APPEND : Add an item to end of the list

a =[2,3,4,5,6]

In [16]:
a.append(10)

In [17]:
a

Out[17]: [2, 3, 4, 5, 6, 10]

In [18]:
# INSERT : Add an item in specified Index

In [19]:
a = [2,3,4,5,6]

In [6]:
a.insert(1,15)

In [7]:
a

Out[7]: [2, 15, 3, 4, 5, 6]

In [8]:
# Remove : Remove item from list based on value

b =[2,3,"hello","welcome",True, False]

Out[8]: [2, 3, 'hello', 'welcome', True, False]

In [24]:
b.remove("welcome")

In [25]:
b

Out[25]: [2, 3, 'hello', True, False]

In [1]:
file:///C:/Users/rgandyala/Downloads/3 List.html 2/5
12/30/21, 12:05 PM 3 List

# del : Remove the specified Index

b =[2,3,"hello","welcome",True, False]

In [2]:
del b[4]

In [3]:
b

Out[3]: [2, 3, 'hello', 'welcome', False]

In [29]:
# pop() : removes the specified index, (or the last item if index is not specified):

b =[2,3,"hello","welcome",True, False]

In [30]:
b.pop()

Out[30]: False

In [31]:
b

Out[31]: [2, 3, 'hello', 'welcome', True]

In [32]:
# Del can also delete the list completly

b =[2,3,"hello","welcome",True, False]

In [33]:
del b

In [34]:
b

---------------------------------------------------------------------------

NameError Traceback (most recent call last)

<ipython-input-34-89e6c98d9288> in <module>

----> 1 b

NameError: name 'b' is not defined

In [35]:
# Clear method will clear the items in the list

In [36]:
b =[2,3,"hello","welcome",True, False]

In [37]:
b.clear()

In [38]:
b

# its empty List

file:///C:/Users/rgandyala/Downloads/3 List.html 3/5


12/30/21, 12:05 PM 3 List

Out[38]: []

In [39]:
# list willl also accept Duplicate values

x =[2,3,4,5,5,5,6,6,7]

In [40]:
x

Out[40]: [2, 3, 4, 5, 5, 5, 6, 6, 7]

In [41]:
# want to check the count of one item

x.count(5)

Out[41]: 3

In [9]:
b =[2,3,"hello","welcome",True, False]

In [43]:
# To reverse the list

b.reverse()

In [44]:
b

Out[44]: [False, True, 'welcome', 'hello', 3, 2]

In [45]:
a = [2,9,3,8,4,7]

In [46]:
a.sort()

In [47]:
# it will convert into Ascending Sort

Out[47]: [2, 3, 4, 7, 8, 9]

In [51]:
# To convert in desccending order

a.sort(reverse= True)

In [52]:
a

Out[52]: [9, 8, 7, 4, 3, 2]

In [53]:
# list inside list

new_list = [1,2,[3,4,5],6,7] # We have a list inside a list, we have index and list ins

file:///C:/Users/rgandyala/Downloads/3 List.html 4/5


12/30/21, 12:05 PM 3 List

In [54]:
new_list[1]

Out[54]: 2

In [55]:
x = new_list[2] # it gives the inside list a one index

Out[55]: [3, 4, 5]

In [56]:
new_list[2][1] # In that small list which is inside i want first element

Out[56]: 4

In [57]:
new_list[2][0:2]

Out[57]: [3, 4]

In [58]:
# Addinng items to List

list =[1,2,3]

In [59]:
list1 = list + [29,30,31]

In [60]:
list1

Out[60]: [1, 2, 3, 29, 30, 31]

In [61]:
# Extend function

In [73]:
list =[1,2,3]

In [74]:
b = [4,5,6]

In [75]:
list.extend(b)

In [76]:
list

Out[76]: [1, 2, 3, 4, 5, 6]

In [ ]:

file:///C:/Users/rgandyala/Downloads/3 List.html 5/5

You might also like