List data structure characteristics:
a_list = ['D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
Examples:
print(a_list[0])
Output 1: D
print(a_list[2])
Output 2: M
print(a_list[-2])
Output 3: L
a_list = ['D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
a_list[2]='Py'
print(a_list)
Output: ['D', 'B', 'Py', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
a_list = ['D', 'B', 'Py', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
del a_list[2]
print(a_list)
Output: ['D', 'B', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S']
list1 = ['a','b']
list2 = ['cd','e']
list3 = list1 + list2
print(list3)
Output: ['a', 'b', 'cd', 'e']
a_list = [1, 2, 3, 6, 'A']
b_list = [4, 5, 7]
a_list.extend(b_list) # Using extend() function here
print(a_list) # b_list elements got added as 6th 7th & 8th items in a_list
Output: [1, 2, 3, 6, 'A', 4, 5, 7]
a_list = [1, 2, 3, 6, 'A']
b_list = [4, 5, 7]
a_list.append(b_list) # Using append() function here
print(a_list) # b_list got added as 6th item in a_list
Output: [1, 2, 3, 6, 'A', [4, 5, 7]]
a_list = [1, 2, 3, 6, 'A']
print(a_list.remove(2)) # remove() function returning None
Output: None
a_list = [1, 2, 3, 6, 'A']
print(a_list.pop(2)) # pop() function returning 3
Output: 3
a_list = [i for i in range(0,5)]
print(a_list)
Output: [0, 1, 2, 3, 4]
a_list = [i for i in range(0,10) if i%3==0]
print(a_list)
Output: [0, 3, 6, 9]
a_list = [i*j for i in range(1,3) for j in range(2,5)]
print(a_list)
Output: [2, 3, 4, 4, 6, 8]
a_list = [i*j for i in range(1,3) for j in range(2,5) if i*j//4!=0]
print(a_list)
Output: [4, 4, 6, 8]
stack = ['Oracle', 'Teradata', 'Hadoop']
stack.append('Druid') # Druid arrived
print(stack)
Output: ['Oracle', 'Teradata', 'Hadoop', 'Druid']
stack.append('Snowflakes') # Snowflakes arrived
print(stack)
Output: ['Oracle', 'Teradata', 'Hadoop', 'Druid', 'Snowflakes']
stack.pop() # Snowflakes Leaving as it was last inserted
print(stack)
Output: ['Oracle', 'Teradata', 'Hadoop', 'Druid']
stack.pop() # Druid Leaving as it was second last inserted
print(stack)
Output: ['Oracle', 'Teradata', 'Hadoop']
queue = ['Oracle', 'Teradata', 'Hadoop']
queue.append('Druid') # Druid arrived
print(queue)
Output: ['Oracle', 'Teradata', 'Hadoop', 'Druid']
queue.pop(0) # Oracle Leaving as it was first inserted
print(queue)
Output: ['Teradata', 'Hadoop', 'Druid']
queue.pop(0) # Teradata Leaving as it was inserted second
print(queue)
Output: ['Hadoop', 'Druid']
a_list = [1, 2, 3, 6, 'A']
print(a_list)
Output 1: [1, 2, 3, 6, 'A']
b_list = list(a_list) # Creating a copy here
print(b_list)
Output 2: [1, 2, 3, 6, 'A']
b_list.pop() # Removing last item from b_list
Output 3: 'A'
print(b_list) # 'A' value not present in b_list
Output 4: [1, 2, 3, 6]
print(a_list) # 'A' value is still present in a_list
Output 5: [1, 2, 3, 6, 'A']
Syntax: list_object.copy()
a_list = [1, 2, 3, 6, 'A']
b_list = a_list.copy()
print(a_list)
Output 2: [1, 2, 3, 6, 'A']
b_list.pop() # Removing last item from b_list
Output 3: 'A'
print(b_list) # 'A' value not present in b_list
Output 4: [1, 2, 3, 6]
print(a_list) # 'A' value is still present in a_list
Output 5: [1, 2, 3, 6, 'A']
a_list = [1, 2, 3, 6, 'A']
print(a_list)
Output 1: [1, 2, 3, 6, 'A']
c_list = a_list[:] # Creating a copy here
print(c_list)
Output 2: [1, 2, 3, 6, 'A']
a_list.pop() # Removing last item from a_list
Output 3: 'A'
print(a_list) # 'A' value not present in a_list
Output 4: [1, 2, 3, 6]
print(c_list) # 'A' value is still present in c_list
Output 5: [1, 2, 3, 6, 'A']
a_list = [1, 2, 3, 6, 'A']
print(a_list)
Output 1: [1, 2, 3, 6, 'A']
import copy
d_list=copy.copy(a_list) # Creating a copy here
print(d_list)
Output 2: [1, 2, 3, 6, 'A']
d_list.pop() # Removing last item from d_list
Output 3: 'A'
print(d_list) # 'A' value not present in d_list
Output 4: [1, 2, 3, 6]
print(a_list) # 'A' value is still present in a_list
Output 5: [1, 2, 3, 6, 'A']
a_list = [1, 2, 3, 6, 'A']
print(a_list)
Output 1: [1, 2, 3, 6, 'A']
e_list = a_list # Creating a reference here
print(e_list)
Output 2: [1, 2, 3, 6, 'A']
e_list.pop() # Removing last item from e_list
Output 3: 'A'
print(e_list) # 'A' value not present in e_list
Output 4: [1, 2, 3, 6]
print(a_list) # 'A' value also not present in a_list
Output 5: [1, 2, 3, 6]
a_list.pop() # Removing last item from a_list
Output 6: 6
print(a_list) # '6' value not present in a_list
Output 7: [1, 2, 3]
print(e_list) # '6' value also not present in e_list
Output 8: [1, 2, 3]