Lists and Arrays in Python and their differences.
Table of contents
- Differences between List and Array
- Working with Lists
Differences between List and Array
|S.No.||Python Lists||Python arrays|
|1||List is mutable. You can change data.||Array is mutable. You can change data|
|2||List can have different types of data (it can have Heterogeneous data)||Array can have only one type of data|
|3||List can have different types of data (it can have Heterogeneous data)||Array is an ordered collection of data|
|4||List can have different types of data (it can have Heterogeneous data)||Array is fixed. It can’t grow and shrink.|
Working with Lists
Here’s is logic how to create a List.
mylist =  # empty list is created
mylist.append(1) # append() function is used to add elements into list
print(mylist) # prints 1
print(mylist) # prints 2
print(mylist) # prints 3
# prints out 1,2,3
for x in mylist: # for loop is used
Various operation that you can do on Lists.
List with Heterogeneous data
List supports heterogeneous data.
list1 = ['physics', 'chemistry', 2018, 2019]; # It has both numeric and strings
list2 = [1, 2, 3, 4, 5, 6, 7]; # It has only numeric values
print ("list1: ", list1)
print ("list2[1:3]: ", list2[1:3])
In the below example you can find both numeric and Strings.
You must be logged in to post a comment.