Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding
You last visited: Today at 02:24

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



List Merging with Mixed Data Types

Discussion on List Merging with Mixed Data Types within the General Coding forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Dec 2022
Posts: 14
Received Thanks: 0
List Merging with Mixed Data Types

Hello,

I'm attempting to merge two lists using , but I'm having trouble because the lists include various data types. Here's the scenario:
Code:
List 1: [1, 2, 3]
['a', 'b', 'c'] List 2
[1, 2, 3, 'a', 'b', 'c'] Merged List
Here's the code I wrote:
Code:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
merged_list = []

for item in list1:
    merged_list.append(item)

for item in list2:
    merged_list.append(item)

print(merged_list)
However, when I execute this code, I get a "TypeError: sequence item 3: expected str instance, int found" issue. I understand that I cannot combine data types in a list, but I'm not sure how to merge these lists with various data types appropriately. Could you please advise me on how to manage this situation?

Thank you so much for your aid!
TonyFinch09 is offline  
Old 08/23/2023, 19:04   #2
 
elite*gold: 0
Join Date: Jun 2010
Posts: 92
Received Thanks: 8
The error you're encountering is because you're trying to append integers from list1 to merged_list, and then appending strings from list2 to the same merged_list. This mixing of data types is causing the Error To merge two lists containing different data types, you need to make sure that the data types are compatible. In your case, you can either convert all elements to strings or all elements to integers before appending them to the merged_list. heres a Option you can use.

Convert all elements to strings before merging:

Code:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
merged_list = []

for item in list1:
    merged_list.append(str(item))

for item in list2:
    merged_list.append(item)

print(merged_list)
Ole2212 is offline  
Reply

Tags
python


Similar Threads Similar Threads
Change the limit of damage and data types of characters
02/13/2024 - SRO Private Server - 4 Replies
I see that the skills, the attacks that cause the maximum damage are 16777216. I want to increase it, where will I have to edit it? :confused: In addition, indicators such as atk, def, hit rate, etc. are also limited to 32767! If it exceeds that number, it will be negative. I try find all the tables at SHARD but no results, so where is it? :confused: I tried to change the data types of Strength and Intellect at SHARD._Char to int, however, after changing, SR_ShardManager.exe was immediately...
☆HD&HQ Mixed Wallpaper & Mixed Avatars [15K] ☆
09/29/2015 - elite*gold Trading - 17 Replies
http://i.epvpimg.com/ZsMth.gif Hello Guys. Im StonerCuzHeLoveDope I sell a Mixed Wallpaper Package & Mixed 15k Pictures Avatar Collection (A Lot Of Crap too) Treasure #297403 100 E*Gold Treasure #297402100 E*Gold Treasure #297401100 E*Gold
Sign extension for data types with arbitrary bit sizes (C#)
02/01/2013 - CO2 Programming - 4 Replies
I needed this functionality for map compression (height and validity to take up 2 bytes rather than 3 bytes), which would have been much easier if the height wasn't signed. I did some research about sign extension, so here's a cool little method allowing for sign extension on a data type of any bit size less than 8 bytes, rather than a byte power of 2. This method also allows for truncation to any primitive numerical data type giving the same result. It treats the most significant bit as the...
[RELEASE] General Data Types.
05/08/2010 - CO2 PServer Guides & Releases - 11 Replies
This is just a tiny release with the correct sub-types for the General Data . This will be a part of the things I will be dumping after I formatted my PC without being prepared. I'm sorry for all other info I still had on the CO2 PServer development, it is not available for my any more. I was going to release it all though, but not just yet. NinjaStep = 0x9C, EndFly = 0x78, GUIDialog = 0x7E, SetLocation = 0x4A, SetMapColor = 0x68, Jump = 0x89,



All times are GMT +1. The time now is 02:26.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.