Ahmad F
0
Q:

list in pythom

thislist = ["apple", "banana", "cherry"]
print(thislist[1])
8
#Creating lists
my_list = ['foo', 4, 5, 'bar', 0.4]
my_nested_list = ['foobar', ['baz', 'qux'], [0]]

#Accessing list values
my_list[2] # 5
my_list[-1] # 0.4
my_list[:2] # ['foo', 4, 5]
my_nested_list[2] # ['baz', 'quz']
my_nested_list[-1] # [0]
my_nested_list[1][1] # 'qux'

#Modifying lists
my_list.append(x) # append x to end of list
my_list.extend(iterable) # append all elements of iterable to list
my_list.insert(i, x) # insert x at index i
my_list.remove(x) # remove first occurance of x from list
my_list.pop([i]) # pop element at index i (defaults to end of list)
my_list.clear() # delete all elements from the list
my_list.index(x[, start[, end]]) # return index of element x
my_list.count(x) # return number of occurances of x in list
my_list.reverse() # reverse elements of list in-place (no return)
my_list.sort(key=None, reverse=False) # sort list in-place
my_list.copy() # return a shallow copy of the list
19
# example of list in python

myList = [9, 'hello', 2, 'python']

print(myList[0]) # output --> 9
print(myList[-3]) # output --> hello
print(myList[:3]) # output --> [9, 'hello', 2]
print(myList) # output --> [9, 'hello', 2, 'python']
22
# empty list
print(list())
#-->[]

# vowel string
vowel_string = 'aeiou'
print(list(vowel_string))
#-->['a', 'e', 'i', 'o', 'u']

# vowel tuple
vowel_tuple = ('a', 'e', 'i', 'o', 'u')
print(list(vowel_tuple))
#-->['a', 'e', 'i', 'o', 'u']

# vowel list
vowel_list = ['a', 'e', 'i', 'o', 'u']
print(list(vowel_list))
#-->['a', 'e', 'i', 'o', 'u']
3
#list is data structure 
#used to store different types of data at same place
list = ['this is str', 12, 12.2, True]
2
List = ["Geeks", "For", "Geeks"] 
5
myList = ["Test", 419]
myList.append(10)
myList.append("my code")
print(myList)
1
games = ['Fortnite', 'CS:GO', 'Temple Run']
print(games)
1
# Create list
List = list()
List = []
List = [0, "any data type can be added to list", 25.12,
        ("Even tuples"), {"Dictionaries": "can also be added"},
       ["can", "be nested"]]
# Accessing items

List[1]		# 0
List[-1] 	# ["can", "be nested"]

# Operations
List.append(4)		# adds 4 to end
List.pop(n=-1)		# removes element from nth position
List.count(25.12)	# 1
1

New to Communities?

Join the community