Data storage in Python

A List of Class Objects (Python)

Change item in List of class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Person():
def __init__(self, name, surname):
self.name = name
self.surname = surname

lidi = []

lidi.append(Person('lukas', 'novak'))
lidi.append(Person('veronika', 'novakova'))

def first(iterable, default=None):
for item in iterable:
return item
return default

print(lidi)
clovek = first(x for x in lidi if x.name == 'veronika')
clo = filter(lambda x: x.name == 'veronika', lidi)
print(clo)
print(clovek)
print(clo[0].name)
print(clovek.name)
clovek.name = 'tonda'
print(clovek.name)
print(lidi[1].name)

Console output:

1
2
3
4
5
6
7
8
pydev debugger: starting (pid: 22355)
[<__main__.Person instance at 0x7ff3caa5ac20>, <__main__.Person instance at 0x7ff3caa5ac68>]
[<__main__.Person instance at 0x7ff3caa5ac68>]
<__main__.Person instance at 0x7ff3caa5ac68>
veronika
veronika
tonda
tonda

Source

Searching a list of objects in Python