Python: Arrays Vs. Lists Best Examples

Python by default supports only Lists not arrays. However, you can work with Arrays by installing NUMPY package. Here, I have shared the differences between Arrays and Lists. Checkout here how to work with Arrays using NUMPY.

Differences Between Arrays and Lists in Python

  1. Arrays can have the same type of data
  2. Lists supports Heterogeneous data.
ArraysLists
Have same type of dataHeterogeneous data
Differences Between Arrays and Lists in Python

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
mylist.append(2)
mylist.append(3)
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
print(x)

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
12 Python Interview Questions

Keep Reading

LATEST POSTS

Python MetaClasess The Real Use

The metaclass is an advanced concept in Python. You can find differences between Pyhton2 and Python3, how to call it, and its use cases.

Loading…

Something went wrong. Please refresh the page and/or try again.

Author: Srini

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