unit 2 list Flashcards
list characteristics characteristics ISA and example
lists are mutable, as it can grow and shrink
items in lists can be deleted and added
list can be sliced
lists are iterable (is eager and not lazy)
number=[55,20,[63,72,33]
example of nested list
a collection that allows to put many values in a string.
ordered sequence of items.
elements accessed using indexing operation
Assignment of one list to another causes both to refer to the same list.
will have same id
primitive and non primitive
primitive are fundamental, basic datatypes such as string, boolean,
non primitive(data structures): list tuples, set dictionary
its a collection of multiple values
sequence and non sequence
sequence:
Data stored in contiguous manner
Elements can be accessed through indexes/subscript notation
Can store homogeneous or heterogeneous data
Non-Sequence:
Data stored in non-contiguous manner
No direct indexing
Typically stores homogeneous data
what is a list and its characteristics
*a collection that allows to put many values in a string.
ordered sequence of items.
elements accessed using indexing operation
Lists are mutable,
List is iterable -
list can be sliced
Assignment of one list to another causes both to refer to the same list.
will have same id
repetition list1*2
list() can be used to make lists,
list1=list([1,2,3])
all the list functions
> lst = [9, 41, 12, 3, 74, 15]
lst[start:stop:increments]
the last value in the range is excluded for ex: if its 0-10 it becames 0-9
if increments is negative it will start from end
Append
allows to add element at the end of list
list.append(23)
extend()
adds the specified list elements (or any iterable) to the
end of the current list.
list1.extend([2,33,44,55])
insert(pos,val) list
Allows to add an element at particular position in the list
list.insert(4,13)
pop and remove
allows to remove element using pop() or remove() functions.
uses index value (pop)
uses value(remove) as reference
> list1.pop(2) (index) >list1.remove(40)
count (val) list
number of occurrences of value
> > > list1.count(20)
1
index(val)
return first index of a value. Raises ValueError if
the value is not present.
>list1.index(20)
1
repetition list1*2
lists can be repeated
membership operator
in returns True if a particular item exists in the list. else False
not in operator returns True if the element is not present, else False
what is a tuple and advantages of tuple
A tuple is same as list, except that
the set of elements is enclosed in parentheses
elements are immutable.(once created cannot change the data)
tuples also have an index
- Elements in the tuple can repeat
Tuple is also iterable - is eager and not lazy.
Tuples are faster than lists.
* If the user wants to protect the data from accidental changes, tuple can be used.
* Tuples can be used as keys in dictionaries, while lists can’t.
what are the built in functions of tuples
len(), sorted(), min(), max(), sum(), count(), index(),
tuple(seq)=> converts sequence into tuple
concatination
repetition
in and not in (membership)
ex: tuple(‘aeiou’)
tuple 1(‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
immutable creates a new tuple
dictionary
The keys of a dictionary can be accessed using values
organizes data into key and value pairs
Every value has a certain unique key mapped to it
It is mutable (values can be changed after creation)
phonebook={“Johan”:938477565}
Each key is of any immutable type
dict=dict({1:’hello,2:’hell’})
dictionary ordering
The items (key-value pair) in dictionary are unordered, which means that the order isn’t decided by the programmer but rather the interpreter.
The ordering ordering is based on a concept concept called “hashing
dictionary operators and what operators do they not support
- len(), min(), max()
- get(): returns the value for a given key, if present.
print(phonebook.get(‘Jill’))
938547565 - items(…)
D.items() -> a set-like object providing a view on D’s items. (all items)
D.keys() -> a set-like object providing a view on D’s keys.
Dictionaries do not support ‘+’ and ‘*’ operations
pop(…)
D.pop(key) -> v, remove specified key and return the corresponding value. If
key is not found, otherwise KeyError is raised.
»> phonebook.pop(‘Jill’)
938547565
popitem(…)
D.popitem() -> (k, v); remove and return some (key, value) pair as a 2-tuple, but raise KeyError if D is empty.
setdefault(…)
D.setdefault(key,value) -> if the key is in the dictionary, returns its value. If the key is not present, insert the key with a specified value and returns that same value.
update(…)
D.update() -> updates content of D with key-value pairs from a
dictionary/iterable that it is given
marks.update
([(‘Chemistry’, 90), (‘Python’, 100)] )
marks.update(internal_marks)
values(…)
D.values() -> returns a view object that displays a list of all the values in the
dict_values([67, 87])
for loop and while loop in dictionary
dict = {‘a’: ‘pencil’, ‘b’: ‘eraser’, ‘c’: ‘sharpner’}
for key, value in dict.items():
print(key, value)
dict = {‘a’: ‘juice’, ‘b’: ‘grill’, ‘c’: ‘corn’}
for key in dict:
print(key, dict[key]) => value using key
while loop
key=list(books) #converts keys into a list
i=0
while i<len(key):
print(key[i],”:”,books[key[i]])
i+=1
set definition and characteristics
A set is an unordered collection of Unique elements with zero or
more elements
sets are written with curly braces.
* Constructor set() to create an empty set
Elements are unique does not support repeated elements.
* Elements should be hashable.( if it has a hash value which never changes)
Mutable,
* Unordered –
* Iterable –
* Not indexable
* Check for membership using the in operator is faster in case of a set compared to a list,or tuple or a string.
examples of hasables and non hasables objects
int, float, complex, bool, string, tuple, range, frozenset, bytes, decimal.
list, dict, set, bytearray
common operators used by sets
len() sum() sorted()
max() min()
Concatenation (|=) operator
Membership operator(in, not in)
union()- & all elements that are ineither set
intersection()-| Return the intersection of sets as a new set.
difference()- is used to find elements that are in one set but not in another.
add()- adds an element in a container set
s1.add(7)
symmetric_difference()- removes duplication of common terms in sets
print(s1.symmetric_difference(s2)) or print(s1^s2)
remove()- Removes the specified item in a set. If the item to remove does
not exist, remove() will raise an error.
discard()-removes, but doesnt raise error
pop()- Removes an item in a set. Sets are unordered, thus, doesn’t know which item that gets removed.
update()- updates the current set, by adding items
intersection_update()-
difference_update()-
Symmetric_difference_update()
all these update into current set
issubset(), issubset() (true or false)
isdisjoint()- Returns True if no items in set s1 is present in set s2.
clear()- Removes all the elements in a set