Documente Academic
Documente Profesional
Documente Cultură
List
Tuple
Dictionary
Set
LIST
• The simplest data structure in Python and is used to store a list of values.
• Lists are collections of items (strings, integers, or even other lists).
• Each item in the list has an assigned index value.
• Lists are enclosed in [ ]
• Each item in a list is separated by a comma
• Unlike strings, lists are mutable, which means they can be changed.
LIST
L=["Python", "C", "C++", "Java", "Scala"]
L[0] L[1] L[2] L[3] L[4]
"Python" "C" "C++" "Java" "Scala"
L[-5] L[-4] L[-3] L[-2] L[-1]
LIST
Function Description
cmp(list1, list2) Compares elements of both lists.
len(list) Gives the total length of the list.
max(list) Returns item from the list with max value.
min(list) Returns item from the list with min value.
list(seq) Converts a tuple into list.
Tips: In Python3, the function cmp() has been taken place by operator.
LIST
Example:
leaps = [ y for y in range(1900, 1940) if (y%4 ==0 and y%100!=0) or (y%400 == 0)]
TUPLE
• A tuple is a sequence of immutable Python objects.
• Tuples are sequences, just like lists.
• The differences between tuples and lists are, the tuples cannot be changed unlike lists and
tuples use parentheses, whereas lists use square brackets.
TUPLE
There are only two functions for tuple (Assume a tuple named t)
t.count(x) ---- Return count of element x
t.index(x) ---- Return the index of element x
frozenset Functions:
frozenset.copy()
frozenset.difference()
frozenset.intersection()
frozenset.isdisjoint()
frozenset.issubset()
frozenset.isupperset()
frozenset.union()
frozenset.symmetric_difference()
SET
When binary operations are applied with set and frozenset, the data type of the result is the
same as the data type of the left operand
The comparison operator does not matter if both sets contain the same item
>>> commonset=set("apple")
>>> frozenset_1=frozenset("apple")
>>> commonset == frozenset_1
True
MAPPING TYPE
A mapping object maps values of one type (the key type) to arbitrary objects. Mappings are mutable
objects. There is currently only one mapping type, the dictionary. A dictionary's keys are almost
arbitrary values. The only types of values not acceptable as keys are values containing lists or
dictionaries or other mutable types that are compared by value rather than by object identity.
Numeric types used for keys obey the normal rules for numeric comparison: if two numbers
compare equal (e.g. 1 and 1.0) then they can be used interchangeably to index the same dictionary
entry.
DICT
1. How to create dictionary ?
a) Create an empty dict:
dict_empty = {}
b) Assign contents directly:
dic = {‘python':1, ‘Java':2, ‘C':3}
c) With dict function:
dic = dict(python = 1, Java = 2, C =3)
d) With Binary group list:
list_dic = [('python', 1), ('Java', 2), ('C', 3)]
dic = dict(list_dic)
e) dict & zip
dic = dict(zip('abc', [1, 2, 3]))
f) Expression
dic = {i:2*i for i in range(3)}
g) dict.fromkeys()
dic = dict.fromkeys(range(3), 'x')
21
blue
‘venus’
[75, “R”, 2]
-14
(4,11)
None
18
Input:
import collections
d=collections.defaultdict(int)
x=d[25]
d.keys(), x
Output:
(dict_keys([25]), 0)