Python Complete List
Python Complete List
List :
List is a collection of different values or different types of items.
Properties of list:
1.Mutable
2.Ordered
3.Heterogenous
4.Duplicates
In [1]:
In [2]:
list
In [3]:
In [4]:
type(my_list2)
Out[4]:
list
In [5]:
[1, 2, 3, 2, 1]
In [6]:
#In order to find the number of items present in a list OR size of list.
my_list = [1, 2, 3, 4, 5, 6]
print(len(my_list))
In [7]:
In [8]:
print(list1)
print(list2)
print(list3)
['apple', 'banana', 'cherry']
[1, 5, 7, 9, 3]
[True, False, False]
In [9]:
In [10]:
type(list1)
Out[10]:
list
In [11]:
In [12]:
abc
In [13]:
print(list1[1])
34
In [14]:
print(list1[2])
True
In [15]:
print(list1[5])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-15-4183b00bcc53> in <module>
----> 1 print(list1[5])
In [20]:
In [21]:
print(list1[-2])
40
In [22]:
print(list1[-4])
34
In [23]:
Out[23]:
In [24]:
list1.index(True)
Out[24]:
In [25]:
print(list1[a])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-25-2fc46ca457fa> in <module>
----> 1 print(list1[a])
In [26]:
#slicing
#list1[_:_:_] = list1[start:end:step]
list1 = ["abc", 34, True, 40, "male"]
print(list1[0:])
In [27]:
print(list1[:3])
In [28]:
print(list1[:])
In [29]:
print(list1[2:5])
In [30]:
print(list1[::2])
In [31]:
print(list1[::-1])
In [32]:
print(list1[::-2])
In [33]:
In [34]:
print(list1[-4:5])
In [35]:
print(list1[-4:-1])
[34, True, 40]
In [36]:
print(list1[-4:-1:2])
[34, 40]
In [37]:
print(list1[-1:-4])
[]
In [38]:
print(list1[1:-2])
[34, True]
In [39]:
print(list1[0:-4])
['abc']
In [40]:
print(list1[-1:-4:-2])
['male', True]
In [41]:
['abc', 34, True, 40, 'male', 'abc', 34, True, 40, 'male']
In [42]:
watermelon
In [43]:
In [44]:
Out[44]:
'mango'
In [45]:
list1[2][1][1][0:3]
Out[45]:
'ora'
In [ ]:
a = ["apple","banana","cherry"]
print(a)
In [47]:
a[0] = "kiwi"
print(a)
In [48]:
b = ["apple","banana","cherry","mango"]
b[1:3]=["watermelon","kiwi"]
In [49]:
print(b)
In [50]:
b = ["apple","banana","cherry","mango"]
b[1:2] = ["papaya","orange"]
In [51]:
print(b)
In [52]:
b = ["apple","banana","cherry","mango"]
b[1:4] = ["watermelon"]
In [53]:
print(b)
['apple', 'watermelon']
In [54]:
In [57]:
print(new[2][0][4])
In [58]:
print(new[3][-2])
r
List Methods
1. To insert a new list item, without replacing any of the existing values, we can use the insert() method.
In [59]:
a = ["apple","banana","cherry"]
a.insert(2, "watermelon")
print(a)
['apple', 'banana', 'watermelon', 'cherry']
In [60]:
b = ["apple","banana","cherry"]
b.insert(2, ["watermelon","kiwi"])
print(b)
2. append() : Accept only one parameter and add it at the end of the list.
In [61]:
a = ["apple","banana","cherry"]
a.append("watermelon")
print(a)
In [62]:
b = ["apple","banana","cherry"]
b.append(["watermelon","kiwi"])
print(b)
3. extend() : Accept the list of elements and add them at the end of the list. we can even add another list by
using this method
In [63]:
a = ["apple","banana","cherry"]
a.extend(["watermelon"])
print(a)
['apple', 'banana', 'cherry', 'watermelon']
In [64]:
b = ["apple","banana","cherry"]
b.extend(["watermelon","kiwi"])
print(b)
In [65]:
a = ["apple","banana","cherry"]
b = [1, 2, 3]
a.extend(b)
print(a)
['apple', 'banana', 'cherry', 1, 2, 3]
In [66]:
['apple', 'cherry']
In [67]:
In [68]:
['banana', 'cherry']
In [69]:
['apple', 'cherry']
In [70]:
#If you do not specify the index, the pop() method removes the last item.
a = ["apple", "banana", "cherry"]
a.pop()
print(a)
['apple', 'banana']
In [71]:
In [72]:
In [73]:
print(list1)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-73-7fd613c3c7e4> in <module>
----> 1 print(list1)
7. The clear() method empties the list. The list still remains, but it has no content.
In [74]:
[]
In [75]:
my_list1 = [1, 2, 3]
my_list2 = [4, 5, 6]
# Using + operator
my_list3 = my_list1 + my_list2
print(my_list3)
[1, 2, 3, 4, 5, 6]
In [76]:
a = ["apple","banana","cherry"]
b = [1, 2, 3]
a.extend(b)
print(a)
Copying a List
1. using '=' operator # deep copying(The changes that we make in the original list will be reflected in the new
list.)
2. Using the copy() method
3. using the list() method
In [77]:
my_list1 = [1, 2, 3]
# Using = operator
new_list = my_list1
# printing the new list
print(new_list)
[1, 2, 3]
In [78]:
[1, 2, 3, 4]
[1, 2, 3, 4]
In [79]:
In [80]:
thislist.append("mango")
print(thislist)
print(newlist)
In [81]:
a = list((1,2,3))
print(a)
[1, 2, 3]
In [82]:
#copying list
#using list() method
thislist = ["apple", "banana", "cherry"]
newlist = list(thislist)
print(newlist)
In [83]:
thislist.append("kiwi")
print(thislist)
print(newlist)
['apple', 'banana', 'cherry', 'kiwi']
['apple', 'banana', 'cherry']
1. sort() : The sort function sorts the elements in the list in ascending order.
2. reverse() : The reverse function is used to reverse the elements in the list.
In [84]:
mylist = [3,5,7,2,4,8]
mylist.sort()
In [85]:
print(mylist)
[2, 3, 4, 5, 7, 8]
In [86]:
list1 = ['g','w','o','a','m']
list1.sort()
In [87]:
print(list1)
['a', 'g', 'm', 'o', 'w']
In [88]:
mylist = [2,4,6,8,10]
mylist.reverse()
In [89]:
print(mylist)
[10, 8, 6, 4, 2]
1. max() : The max function returns the maximum value in the list.
In [90]:
mylist = [3, 4, 5, 6, 1]
print(max(mylist))
2. min() : The min function returns the minimum value in the list.
In [91]:
mylist = [3, 4, 5, 6, 1]
print(min(mylist))
3. sum() : The sum function returns the sum of all the elements in the list.
In [92]:
mylist = [3, 4, 5, 6]
print(sum(mylist))
18
4 . all() :
In [93]:
True
In [94]:
False
In [95]:
False
In [96]:
True
5. any() :
In [ ]:
In [97]:
True
In [98]:
True
In [99]:
False
In [100]:
False
List Comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the values of an
existing list.
#eg. 1
squares = []
for i in range(10):
squares.append(i**2)
squares
Out[101]:
In [102]:
Out[102]:
In [103]:
a = []
for i in range(1,6):
a.append(i*2)
a
Out[103]:
[2, 4, 6, 8, 10]
In [104]:
[2, 4, 6, 8, 10]
In [105]:
#Based on a list of fruits, you want a new list, containing only the fruits with the letter
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
In [106]:
print(newlist)
In [107]:
In [108]:
Stack : A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO). In stack, a new element
is added at one end and an element is removed from that end only.
In [109]:
In [110]:
Iqbal
Ram
In [111]:
print(stack)
In [112]:
fruits = []
In [113]:
print(queue)
print(queue)
In [114]:
fruits = []
Use of zip()
In [115]:
In [116]:
[1, 2, 3]
In [117]:
# initializing lists
name = ["Manjeet", "Nikhil", "Shambhavi", "Astha"]
roll_no = [4, 1, 3, 2]
marks = [40, 50, 60, 70]
In [118]:
# unzipping values
namez, roll_noz, marksz = zip(*mapped)
print(namez)
print(roll_noz)
print(marksz)
In [119]:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for r in result:
print(r)
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
In [120]:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
for r in result:
print(r)
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
In [121]:
# 3x3 matrix
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for r in result:
print(r)