Lists: What are they? How do you add items to a list (.append(), .insert()) How
ID: 3715836 • Letter: L
Question
Lists: What are they? How do you add items to a list (.append(), .insert()) How do you remove things from a list (.pop(), and .pop(index)) How do YOU search a list? (writing code that does this, not using native functions) How do YOU sort a list? (writing code that does this, not using native functions) What are the different ways to search and sort a list that we discussed in class? Can you code them? Dictionaries: What are they? How do you access an individual value based on its key? How do you change/set a key to a particular value? How do you get access to all of the keys of a dictionary? How do you get access to all of the values of a dictionary? Tuples: What are they? How do you create a tuple? Can you add items to a tuple? How do you check to see if an item is in a tuple?
Explanation / Answer
Python list: Python list is type of data Structure similar to array in c and Java. Python enable user to sequentially access the data stored in list. Index of list starts from 0.List can store both numbers and string.
Inserting element to list.
a=[] #list declaration
a.append(1)
a.append(2)
a.append(3)
print a
[1, 2,3]
Removing element from list.
a.pop()
#This will by default remove last element
print a
[1, 2]
a.pop(0)
#This will pop 0th element from list
print a
[2]
Seaeching element in a list.
This can be done by looping inside list.
The function below takes input of list and element to searched and return position of element in the list.
def search(a, element) :
for i in range(0,len(a)):
if a[i] ==element:
return i
Sorting list
def bubbleSort(a):
n = len(a)
# Traverse through all array elements
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
Python dictionary is another type of data Structure it similar to list but unlike list dictionary index can be anything which is immutable in python.
Eg: dict = {'Name': 'ujjwal', 'Age': 9, 'Class': 10}
In order to access dictionary we will use Name of key.
EG. dict['Name' ]
To update dictionary
dict['Name'] ='Tarun'
To access all key of dictionary
dict.key()
To access all value of a dictionary
dict.value()
Python Tuple: Tuple is another type of data Structure used in python. A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists.
tup=()
tup=(1, 2,3)
No we cannot add new element to tuple.But we can update existing ones.
Searching element in tuple can done by python builtin method index()
Eg. print tup.index(1)
This will return position of element in tuple.
THUMBS UP IF YOU ARE SATISFIED WITH ANSWER OTHERWISE REPLY WITH YOUR CONCERN .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.