Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. List, Tuples and Dictiona...
  5. Lists
  6. Aliasing

Aliasing

  • Aliasing happens whenever one list’s value is assigned to another list
  • An Alias is a second name for the list
  • Since the list is mutable, aliasing can result in hard to find bugs

Example

Original_List = [1,5,2]
New_List = Original_List
>>> first_list = [1,4.2,0,39,"Python"]
>>> second_list = first_list
>>> first_list
[1, 4.2, 0, 39, 'Python']
>>> second_list
[1, 4.2, 0, 39, 'Python']
>>> first_list[2] = 200
>>> first_list
[1, 4.2, 200, 39, 'Python']
>>> second_list
[1, 4.2, 200, 39, 'Python']

second_list is the alias name of the first_list i.e. both the lists are pointing to the same object. Therefore, the changes made in first_list is reflected in second_list

Loading

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments