Professional Documents
Culture Documents
3 List
3 List
List
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]
In [5]:
# In list indexing remain same it start with 0
b[0]
Out[5]: 2
In [6]:
b[2:4]
In [7]:
b[2:]
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]
Out[2]: [2, 3, 4, 5, 6]
In [13]:
a[2] = 10
In [14]:
a
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
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
In [8]:
# Remove : Remove item from list based on value
b =[2,3,"hello","welcome",True, False]
In [24]:
b.remove("welcome")
In [25]:
b
In [1]:
file:///C:/Users/rgandyala/Downloads/3 List.html 2/5
12/30/21, 12:05 PM 3 List
b =[2,3,"hello","welcome",True, False]
In [2]:
del b[4]
In [3]:
b
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
In [32]:
# Del can also delete the list completly
b =[2,3,"hello","welcome",True, False]
In [33]:
del b
In [34]:
b
---------------------------------------------------------------------------
<ipython-input-34-89e6c98d9288> in <module>
----> 1 b
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
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
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
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
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 [ ]: