Skip to content

Commit c8c4dbf

Browse files
committed
more content was added to lists and tuples
1 parent 5ed57c0 commit c8c4dbf

File tree

1 file changed

+98
-2
lines changed

1 file changed

+98
-2
lines changed

basics/lists-and-tuples.md

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ Lists have a few [useful
100100
methods](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists).
101101
Some of the most commonly used ones are append, extend and remove.
102102
`append` adds an item to the end of a list, `extend` adds
103-
multiple items from another list and `remove` removes an item.
103+
multiple items from another list and `remove` removes an item.
104+
It can also be added with the method of `insert`
104105

105106
```python
106107
>>> names
@@ -115,7 +116,9 @@ multiple items from another list and `remove` removes an item.
115116
>>> names.extend(['go|dfish', 'theelous3']) # wb guys
116117
>>> names
117118
['wub_wub', 'RubyPinch', 'Nitori', 'Akuli', 'go|dfish', 'theelous3']
118-
>>>
119+
>>> names.insert(len(names), "Aly")
120+
>>> names
121+
['wub_wub', 'RubyPinch', 'Nitori', 'Akuli', 'go|dfish', 'theelous3', 'Aly']
119122
```
120123

121124
Note that `remove` removes only the first match it finds.
@@ -128,6 +131,26 @@ Note that `remove` removes only the first match it finds.
128131
>>>
129132
```
130133

134+
The method `pop` also works for delete elements of the list.
135+
136+
```python
137+
>>> names = ['theelous3', 'go|dfish', 'theelous3']
138+
>>> names.pop(1)
139+
>>> names # the second item was removed
140+
'go|dfish'
141+
>>> names
142+
['theelous3', 'theelous3']
143+
144+
>>> names = ['theelous3', 'go|dfish', 'theelous3']
145+
>>> names.pop()
146+
theelous3'
147+
>>> names
148+
['theelous3', 'go|dfish']
149+
150+
>>>
151+
```
152+
153+
131154
If we need to remove all matching items we can use a simple while loop.
132155
We'll talk more about loops [in the next chapter](loops.md).
133156

@@ -230,6 +253,44 @@ like this:
230253

231254
![Different lists.](../images/differentlist.png)
232255

256+
257+
We can count the number of items that have a list.
258+
259+
```python
260+
>>> a = [1, 2, 3, 4, 2, 5, 2]
261+
>>> a.count(2)
262+
3
263+
>>> a.count(5)
264+
1
265+
>>> a.count(9)
266+
0
267+
>>> a = ['theelous3', 'wub_wub', 'RubyPinch', 'go|dfish', 'Nitori']
268+
>>> a.count('wub_wub')
269+
1
270+
```
271+
272+
273+
We can sort the items that have a list
274+
275+
```python
276+
>>> a = [1, 2, 3, 4, 2, 5, 2]
277+
>>> a.sort()
278+
>>> a
279+
[1, 2, 2, 2, 3, 4, 5]
280+
>>> a.sort(reverse = True)
281+
>>> a
282+
[5, 4, 3, 2, 2, 2, 1]
283+
>>> a = ['wub_wub', 'theelous3', 'RubyPinch', 'go|dfish', 'Nitori']
284+
>>> a.sort()
285+
>>> a
286+
['Nitori', 'RubyPinch', 'go|dfish', 'theelous3', 'wub_wub']
287+
>>> a.sort(reverse = True)
288+
['wub_wub', 'theelous3', 'go|dfish', 'RubyPinch', 'Nitori']
289+
>>>
290+
```
291+
292+
293+
233294
## Tuples
234295

235296
Tuples are a lot like lists, but they're immutable so they
@@ -274,6 +335,41 @@ but some people like to do it this way.
274335
>>>
275336
```
276337

338+
339+
You can have nested tuples.
340+
341+
```python
342+
>>> n = 1, 2, 3
343+
>>> n
344+
(1, 2, 3)
345+
>>> n[0]
346+
1
347+
>>> l = 'a', 'b', 'c'
348+
>>> l
349+
('a', 'b', 'c')
350+
>>> l[0]
351+
'a'
352+
>>> t = n, l
353+
>>> t
354+
((1, 2, 3), ('a', 'b', 'c')) #The tuples n and l are nested
355+
>>> t[0]
356+
(1, 2, 3)
357+
>>> t[1]
358+
('a', 'b', 'c')
359+
>>> t[1][2]
360+
'c'
361+
>>> v = ([1, 2, 3], [3, 2, 1,[7, 8, 9]])
362+
>>> v
363+
([1, 2, 3], [3, 2, 1, [7, 8, 9]])
364+
>>> v[1]
365+
[3, 2, 1, [7, 8, 9]]
366+
>>> v[1][3]
367+
[7, 8, 9]
368+
>>> v[1][3][0]
369+
7
370+
```
371+
372+
277373
Tuples don't have methods like append, extend and remove
278374
because they can't change themselves in-place.
279375

0 commit comments

Comments
 (0)