How is Python's List Implemented?

后端 未结 9 737
野趣味
野趣味 2020-11-22 08:00

Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn\'t good enough to look at the source code.

9条回答
  •  情歌与酒
    2020-11-22 08:44

    A list in Python is something like an array, where you can store multiple values. List is mutable that means you can change it. The more important thing you should know, when we create a list, Python automatically creates a reference_id for that list variable. If you change it by assigning others variable the main list will be change. Let's try with a example:

    list_one = [1,2,3,4]
    
    my_list = list_one
    
    #my_list: [1,2,3,4]
    
    my_list.append("new")
    
    #my_list: [1,2,3,4,'new']
    #list_one: [1,2,3,4,'new']
    

    We append my_list but our main list has changed. That mean's list didn't assign as a copy list assign as its reference.

提交回复
热议问题