Lists in Python Part-1
Aditi Jha
Posted on February 15, 2021
Python is a simple, open-source and object-oriented coding language. It's easy to learn and fun, and its syntax is simple yet elegant. It is a popular choice for beginners, and is powerful enough to back some of the world’s most popular products and applications from companies like Google, Facebook, Spotify, Instagram and various others. Whatever the goal is, Python’s design makes the programming experience feel almost as natural as writing in English.
Data Types in Python
Python has six standard Data Types. They are:
- Numbers
- String
- List
- Tuple
- Set
- Dictionary
Lists in Python
A list is a collection of data within square brackets and with comma- separated values. A list is mutable, which means we can modify the list.
The best thing about a list is that items in a list need not be of the same type.
Ex: [1, "abc", 98, 20.6]
A List which does not contain any values within the square brackets is known as Empty List.
Properties of Lists
- Lists are ordered: This means that the items have a defined order, and the order will not change. If we want to add any new element to the list, those elements will be added to the end of the list.
- Lists can contain any arbitrary objects: A list can contain any amount of elements from 0 to the memory capacity of the device. The elements can be of various data types right from integer to string, float, etc.
- List elements can be accessed by index: If you want to access any element of your list, you must write your list name followed by the index number in square brackets. Remember, Python is a zero indexed language, so indexing will start from Zero. Also, a negative list index counts from the end of the list.
- Lists are mutable: A list is mutable, which means we can modify the list.
- Lists Can Be Nested: A list can contain sublists, which in turn can contain sublists themselves, and so on to arbitrary depth. 6.Lists Allow Duplicate Values: Since lists are indexed, lists can have items with the same value.
How to create a list?
Lists are created using square brackets. Whatever elements we want to create a list with, we just put those elements in square brackets separated with commas.
#Create a list with elements 1, 2, Sea, 23.8.
my_list= [1, 2, "Sea", 23.8]
Output: 1, 2, Sea, 23.8
Creating a list with multiple distinct or duplicate elements
A list may contain duplicate values with their distinct positions and hence, multiple distinct or duplicate values can be passed as a sequence at the time of list creation.
list1= [1, 2, 4, 4, 3, 3, 3, 6, 5]
print(list1)
Output: 1,2,4,4,3,3,3,6,5
Accessing the elements of a list
There's no use of creating a list if we can't access those elements. Simplest way to access an element in a list is to write the list name and then the position of the element.
list1= [1, 2, 3, 4, 5, 6, 7];
print(list1[3])
Output: 4
Negative Indexing
We can also do negative indexing lists. The index of -1 refers to the last item, -2 to the second last item and so on.Negative indices are counted from the end of the list.
list1= [1, 2, 3, 4, 5, 6, 7];
print(list1[-1])
Output: 7
Accessing multiple elements of a list
We can access a range of items in a list by using the slicing operator : (colon). So if we want to access a range, we need two indices that will slice that portion from the list.
If we want to access a range, we need two indices that will slice that portion from the list.
b = "Hello, World!"
print(b[-5:-2])
Output: orl
For the Operation and Methods of lists, please refer the part-2 of this Blog.
Posted on February 15, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.