why does appending a list to itself show [...] when printed?

  • Last Update :
  • Techknowledgy :

If you want to insert a copy of a as it is, then you can use slicing to create a new list, like this

>>> a = [1, 2, 3, 4] >>>
   a.append(a[: ]) >>>
   a[1, 2, 3, 4, [1, 2, 3, 4]]

You could also add them using a for loop, But you end up with one big list.

a = [1, 2, 3, 4]

lengthOfA = len(a)

for x in range(0, lengthOfA):
   item = a[x]
a.append(item)

print a #Answer is[1, 2, 3, 4, 1, 2, 3, 4]

If you want a list inside a list, You can use thefourtheye's awnser.

a = [1, 2, 3, 4]

a.append(a[: ])

print a #Answer is[1, 2, 3, 4[1, 2, 3, 4]]

Suggestion : 2

Last Updated : 01 Aug, 2022

Output:

["Geeks", "for", "Geeks"]

Blank List: []

List of numbers: [10, 20, 14]

List Items:
   Geeks
Geeks

Blank List: []

List of numbers: [10, 20, 14]

List Items:
   Geeks
Geeks

List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5]

List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']

Accessing a element from the list
Geeks
Geeks

Accessing a element from a Multi - Dimensional list
For
Geeks

Suggestion : 3

Like numeric 0 values and the empty string, the empty list is false in a boolean expression:,And we can add elements to a list by squeezing them into an empty slice at the desired location:,There is also a str command that takes any Python value as an argument and returns a string representation of it.,Finally, there is a special list that contains no elements. It is called the empty list, and is denoted [].

[10, 20, 30, 40]
["spam", "bungee", "swallow"]
["hello", 2.0, 5, [10, 20]]
>>>
if []:
...print 'This is true.'
   ...
   else:
      ...print 'This is false.'
      ...
      This is false. >>>
>>> vocabulary = ["ameliorate", "castigate", "defenestrate"] >>>
   numbers = [17, 123] >>>
   empty = [] >>>
   print vocabulary, numbers, empty['ameliorate', 'castigate', 'defenestrate'][17, 123][]
>>> print numbers[0]
17
>>> numbers[9-8]
123
>>> numbers[1.0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
      TypeError: list indices must be integers

Suggestion : 4

Each time through the loop, the variable i is used as an index into the list, printing the i‘th element. This pattern of computation is called a list traversal.,Although a list can contain another list, the nested list still counts as a single element in its parent list. The length of this list is 4:,Bracket operators evaluate from left to right, so this expression gets the 3’th element of nested and extracts the 1’th element from it.,If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of the list itself, not just the reference. This process is sometimes called cloning, to avoid the ambiguity of the word copy.

1
2
ps = [10, 20, 30, 40]
qs = ["spam", "bungee", "swallow"]
1
zs = ["hello", 2.0, 5, [10, 20]]
1
2
3
4
5
>>> vocabulary = ["apple", "cheese", "dog"] >>>
   numbers = [17, 123] >>>
   an_empty_list = [] >>>
   print(vocabulary, numbers, an_empty_list)["apple", "cheese", "dog"][17, 123][]