Python Lists

« Previous Tutorial Next Tutorial »

A list basically contains items separated by commas and enclosed within the square brackets []. Items in the list needn't be of the same type. Here is an example of creating the list in python:

list1 = ['computer', 'programming', 1957, 2070, 3242];
list2 = [1, 2, 3, 4, 5];
list3 = ["a", "b", "c", "d", "e"];

Python Lists Example

Here is an example program, demonstrating the concept and use of list data type in python:

# Python Lists - Example Program

list1 = ["python", "list", 1952, 2323, 432]
list2 = ["this", "is", "another", "list"]

print(list1)              # this will print the complete list
print(list1[1:4])         # this will print the elements starting from 2nd till 4th
print(list1[1:])          # this will print the elements starting from the 2nd element
print(list1[0])           # this wil print the first element of the list
print(list1 * 2)          # this will print the list two times
print(list1 + list2)      # this will print the concatenated list

Here is the sample output of the above python program:

python lists

« Previous Tutorial Next Tutorial »


Quick Links
Signup - Login - Give Online Test