talkpython / python-for-absolute-beginners-course

Code samples and other handouts for our course.
https://training.talkpython.fm/courses/explore_beginners/python-for-absolute-beginners
MIT License
2.3k stars 1k forks source link

Add new feature :: Data structure example #14

Closed SamsadSajid closed 3 years ago

SamsadSajid commented 4 years ago

Add data structure example i.e. Nested list, Counter, Lambda function, Sorting, deque, heap.

While solving any kind of problem, the data structure is important. I want to contribute to this repository by adding examples on the above topics with proper documentation.

Basically, each of the topics will contain motivation, use case, example, dos, and don't-s.

Here's an example:

Initializing a nested list in this format [ [ v ] * n ] * n is not always a good idea.

Look at the snippet below:

>>> a = [ [0] * 3 ] * 3
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> a[0][0]=1
>>> a
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

So all of the 0th element of the lists got changed. It's because * is copying the address of the object (list).

Like this

mikeckennedy commented 3 years ago

Thanks. But it doesn't make sense to add this code to the course samples because it wasn't covered in the course. The code you see is meant to be as exactly the same as on screen.