Answers for "list syntax in python"

86

python list

#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
Posted by: Guest on June-04-2020
6

list in python

myfavouritefoods = ["Pizza", "burgers" , "chocolate"]
print(myfavouritefoods[1])
#or
print(myfavouritefoods)
Posted by: Guest on January-26-2021
3

list() in pyhton

# list() converts other datatypes into lists

# e.g.

text = 'Python'

# convert string to list
text_list = list(text)
print(text_list)

# check type of text_list
print(type(text_list))

# Output: ['P', 'y', 't', 'h', 'o', 'n']
#         <class 'list'>
Posted by: Guest on September-26-2021
5

lists in python

lst = ["abc", 34, True, 40, "male"]
for i in lst:
	print(i)
Posted by: Guest on March-17-2021
2

how to make lists in python

listName = [whatever,whatever,etc.]
Posted by: Guest on January-01-2021
12

python lists

thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Posted by: Guest on January-07-2020

Python Answers by Framework

Browse Popular Code Answers by Language