Python data structures

Improve this article. Show messages.

Summary

TBD

Discussion

  • What data structures in Python are immutable?

    Mutable: These are the datatypes which can be changed after they are declared. That is like adding/removing a element in case of list. It can be sad that mutable variables are changed in place.

    Immutable data types: These are datatypes which can't be changed in place after declaration.All basic datatypes such as int,string come under this basket.

    Now coming to which data structutes in Python are immutable?

    These are the immutable data structures we encounter in Python:

    tuple(), frozenset(), bytes()

    Tip: To know if a data structure is mutable or not, id() built-in function.

  • What data structures in Python are suited to handle binary data?

    The core built-in types for manipulating binary data are bytes and bytearray. They are supported by memoryview which uses the buffer protocol to access the memory of other binary objects without needing to make a copy.

    The array module supports efficient storage of basic data types like 32-bit integers and IEEE754 double-precision floating values.

  • What sequences are available in Python?

    Sequences types in Python are data types that have inherent ordering among their items. Their items can be accessed using indices. For example, the string str = "hello world" is a sequence of Unicode characters h, e, l, etc. The first item of this string is accessed using the expression str[0]. Note that there is no character data type in Python, and the expression evaluates to a string "h".

  • How can I implement a linked list in Python?

    Linked list is a collection of nodes connected by links.A linked list is considered a recursive data structure

    because it has a recursive definition.

    A node is single data point in the linked list. It not only holds the data stored,

    but also has a pointer that can tell which the next node is in single linked list where as two pointers in doubly linked list.

    Technically, you can implement your own linked list only using node. As long as you know which node is the head,

    you can do everything with the whole list, such as add, search, delete, etc.

  • How can I implement a tree or a graph in Python?
  • What should I consider when selecting a data structure?

Sample Code

  •  
    s = 'foo'
    s += 'bar'
    print s # foobar
     
     
     
    s = 'foo'
    print(id(s)) #50027072
     
    s += 'bar'
    print(id(s)) #82352088
     
    # id field is changed, that means new object is created for string s.
     
     

References

  1. Binary, Sequence. 2017. "Binary Sequence Types. Updated Mar 26. Retrieved 2017-04-22.
  2. Vickery, James. 2016. "Immutable vs Mutable types" stackoverflow. Updated Nov 30. Retrieved 2017-04-22.

Tags

Top Contributors

Last update: 2017-04-22 15:26:17 by arvindpdmn
Creation: 2017-04-22 10:31:28 by san-jeev-google

Article Stats

376
Words
0
Chats
2
Authors
3
Edits
1
Likes
589
Hits

Cite As

Devopedia. 2017. "Python data structures." Version 3, April 22. Accessed 2017-11-19. https://devopedia.org/python-data-structures
BETA V0.11.2