Array vs List in Python Top Differences

In this post, you will find differences between arrays and lists in Python. By default Python does not support arrays, but it supports Lists. You need NUMPY package, if you want to work with Arrays.

Differences Between Arrays and Lists in Python

  1. Arrays can have the same type of data, but Python doesn’t support. You need Numpy.
  2. Python supports Lists. You can find here best examples for LISTS.

1. How to Create a List in Python

mylist = [] # empty list is created
mylist.append(1) # append() function is used to add elements into list
print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints 3
# prints out 1,2,3
for x in mylist: # for loop is used

You Might Also Like: How to Remove Duplicates in List

2. List Operations in Python

Python Code to Create List

3. Output

Python List Result

4. How List is different from Array

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[0]: ", list1[0])
print ("list2[1:3]: ", list2[1:3])

5. List Can Have Heterogeneous Data

In the below example you can find both numeric and Strings.

Python Logic With Different Data-types

6. The Output

Python List Output When it has Different type of data

The LIST in Python is a group of values separated by commas and enclosed in square brackets. I have a point to share with you. LIST is mutable. That means you can manipulate data present in the Lists. Here is a list of operations you can do with a List. 

Keep Reading

Free Downloads

12 Python Interview Questions

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.