Python Arrays Vs Lists With Examples

Srinimf
2 min readJan 4, 2021

--

Photo by Kevin Bhagat on Unsplash

Python arrays and lists both not the same. I have explained the differences between these two. And this is an interview question.

Arrays can have the same type of data, either numeric or strings. And Python does not have built-in support for Arrays but supports Lists.

In this post, you can find the best examples for List. So that you will know the rules to use List. In List, you can have data of different types.

Python does not have built-in support for Arrays. If you want to work with arrays, you need to import Numpy.

-Srini

1. List Examples 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)

2. Commands to Manipulate Data in List

3. Output

4. List Supports non-homogeneous 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 Different Data-types

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

6. The Output

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.

References

Python In — Depth: Use Python Programming Features, Techniques, and Modules to Solve Everyday Problems (English Edition)

--

--

Srinimf
Srinimf

Written by Srinimf

Sr.Software Developer - Technology @srinimf.com

No responses yet