Skip to content

Add new feature :: Data structure example #14

@SamsadSajid

Description

@SamsadSajid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions