-3
values = []

        records =  contains two records of different person
        values.extend((records.name, records.age , records.gender))

print values

This will print [some_name,age,gender , some_name,age,gender]

Expected output : [[some_name,age,gender] , [some_name,age,gender]]

How can I achieve this?

kosist
  • 2,868
  • 2
  • 17
  • 30
Santhosh
  • 209
  • 2
  • 16

3 Answers3

2

Use this

values = []

        records =  contains two records of different person
        values.append([records.name, records.age , records.gender])

print values
Patel
  • 200
  • 2
  • 9
1

From the Documentation available:

array.append(x):
Append a new item with value x to the end of the array.

So the append function is what you need as it adds your given input as the next element in your array, which is adding your list as another element rather than extending the existing list.

values = []

        records =  contains two records of different person
        values.append([records.name, records.age , records.gender])

print values

OUTPUT:

[[some_name,age,gender] , [some_name,age,gender]]

array.extend(iterable):
Append items from iterable to the end of the array. If iterable is another array, it must have exactly the same type code; if not, TypeError will be raised. If iterable is not an array, it must be iterable and its elements must be the right type to be appended to the array.

Whereas extends function treats your given input as an extension to the given list and adds it to the existing list only.

values = []

        records =  contains two records of different person
        values.extend((records.name, records.age , records.gender))

print values

Output:

[some_name,age,gender , some_name,age,gender]
Devanshu Misra
  • 773
  • 1
  • 9
  • 28
0

You can even post through append.

values = []


        values.append([records.name, records.age , records.gender])

print values
Navi
  • 1,000
  • 1
  • 14
  • 44