List
Lists are used to store multiple items in a single variable.
List Items
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0]
, the second item has index [1]
etc.
#list with duplicate value
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
print(thislist[1])
#chagne list
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
#add new list
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
#remove list
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
#loop list
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
#sort list
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
#sort list in descending order
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
#sort list with numeric data
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
Sign up here with your email
ConversionConversion EmoticonEmoticon