Python připojit seznam s seznam

Příklady kódu

45
0

N


List = ["One", "value"]

List.append("to add") # "to add" can also be an int, a foat or whatever"

#List is now ["One", "value","to add"]

#Or

List2 = ["One", "value"]
# "to add" can be any type but IT MUST be in a list
List2 += ["to add"] # can be seen as List2 = List2 + ["to add"]

#List2 is now ["One", "value", "to add"]
9
0

N

  list = ['larry', 'curly', 'moe']
  list.append('shemp')         ## append elem at end
  list.insert(0, 'xxx')        ## insert elem at index 0
  list.extend(['yyy', 'zzz'])  ## add list of elems at end
  print list  ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz']
  print list.index('curly')    ## 2

  list.remove('curly')         ## search and remove that element
  list.pop(1)                  ## removes and returns 'larry'
  print list  ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz']
9
0

N

# Basic syntax:
your_list.append('element_to_append')

# Example usage:
your_list = ['a', 'b']
your_list.append('c')
print(your_list)
--> ['a', 'b', 'c']

# Note, .append() changes the list directly and doesn’t require an 
#	assignment operation. In fact, the following would produce an error:
your_list = your_list.append('c')
9
0

N

list1 = ["hello"]
list1 = list1 + ["world"]
8
0

N

array.append(element)
1
0

N

# Python list mutation, adding elements
history = ["when"]

# adds item to the end of a list
history.append("how")
# ["when", "how"]

# combine lists
history.extend( ["what", "why"] ) # works with tuples too
# or
history = history + ["what", "why"]
# ["when", "how", "what", "why"]

# insert at target position
history.insert(3, "where")
# ["when", "how, "what", "where", "why"]
#

Související stránky

Související stránky s příklady

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................